Path: csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.007 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'parameter': 0.07; "'w',": 0.09; 'dec': 0.15; "skip:' 30": 0.15; "'0',": 0.16; "'a',": 0.16; "'b',": 0.16; "'c',": 0.16; "'d',": 0.16; "'e',": 0.16; "'o',": 0.16; "'r',": 0.16; "'z')": 0.16; '24,': 0.16; 'mon,': 0.16; 'wrote:': 0.17; '>>>': 0.18; 'appropriate': 0.20; 'sort': 0.21; 'import': 0.21; 'random': 0.24; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; "skip:' 20": 0.32; 'to:addr:python- list': 0.33; 'mapping': 0.35; 'option': 0.37; 'subject:: ': 0.38; 'received:10': 0.38; 'to:addr:python.org': 0.39; 'content- disposition:inline': 0.60; '8bit%:50': 0.65; "'2',": 0.84; "'3',": 0.84; 'order:': 0.84; 'received:10.94': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=uni-mainz.de; i=@uni-mainz.de; q=dns/txt; s=ironport; t=1356365474; x=1387901474; h=date:from:to:subject:message-id:references:mime-version: content-transfer-encoding:in-reply-to; bh=n1SHupLsb4Duq7pZ22igpEYuHvqYFZ64Mt59BTKvPpc=; b=OV/92A4KmTMiauxN5kaKHO+oY+eWOOP0dC+wyZirk0Ga1eMM6qjiBsjq dT95pgE+lUXMKEKCAt3Bl/Ebq7nuUDjbC9/FePlVnmJ385tzb/4A6H8KR 6kAwjxqbCJZk6BNQ2BG4DRtTvLCW074oPoNDY2cB7PweWR4PexJdSE+4/ U=; X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: ArwGANV92FAKXgZY/2dsb2JhbABEg0iCGLgcc4IeAQEEAQwmAVYJAhguFCkgiCAKiF2tDYxXgRqCSGEDiC2KLIMyAZBIgnWCIQ Date: Mon, 24 Dec 2012 17:11:03 +0100 From: Thomas Bach To: Subject: Re: Custom alphabetical sort References: <40d108ec-b019-4829-a969-c8ef513866f1@googlegroups.com> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <40d108ec-b019-4829-a969-c8ef513866f1@googlegroups.com> User-Agent: Mutt/1.5.21 (2010-09-15) X-Originating-IP: [88.68.6.180] X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 20 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1356365475 news.xs4all.nl 6982 [2001:888:2000:d::a6]:55035 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:35463 On Mon, Dec 24, 2012 at 07:32:56AM -0800, Pander Musubi wrote: > I would like to sort according to this order: > > (' ', '.', '\'', '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'A', 'ä', 'Ä', 'á', 'Á', 'â', 'Â', 'à', 'À', 'å', 'Å', 'b', 'B', 'c', 'C', 'ç', 'Ç', 'd', 'D', 'e', 'E', 'ë', 'Ë', 'é', 'É', 'ê', 'Ê', 'è', 'È', 'f', 'F', 'g', 'G', 'h', 'H', 'i', 'I', 'ï', 'Ï', 'í', 'Í', 'î', 'Î', 'ì', 'Ì', 'j', 'J', 'k', 'K', 'l', 'L', 'm', 'M', 'n', 'ñ', 'N', 'Ñ', 'o', 'O', 'ö', 'Ö', 'ó', 'Ó', 'ô', 'Ô', 'ò', 'Ò', 'ø', 'Ø', 'p', 'P', 'q', 'Q', 'r', 'R', 's', 'S', 't', 'T', 'u', 'U', 'ü', 'Ü', 'ú', 'Ú', 'û', 'Û', 'ù', 'Ù', 'v', 'V', 'w', 'W', 'x', 'X', 'y', 'Y', 'z', 'Z') > One option is to use sorted's key parameter with an appropriate mapping in a dictionary: >>> cs = (' ', '.', '\'', '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'A', 'ä', 'Ä', 'á', 'Á', 'â', 'Â', 'à', 'À', 'å', 'Å', 'b', 'B', 'c', 'C', 'ç', 'Ç', 'd', 'D', 'e', 'E', 'ë', 'Ë', 'é', 'É', 'ê', 'Ê', 'è', 'È', 'f', 'F', 'g', 'G', 'h', 'H', 'i', 'I', 'ï', 'Ï', 'í', 'Í', 'î', 'Î', 'ì', 'Ì', 'j', 'J', 'k', 'K', 'l', 'L', 'm', 'M', 'n', 'ñ', 'N', 'Ñ', 'o', 'O', 'ö', 'Ö', 'ó', 'Ó', 'ô', 'Ô', 'ò', 'Ò', 'ø', 'Ø', 'p', 'P', 'q', 'Q', 'r', 'R', 's', 'S', 't', 'T', 'u', 'U', 'ü', 'Ü', 'ú', 'Ú', 'û', 'Û', 'ù', 'Ù', 'v', 'V', 'w', 'W', 'x', 'X', 'y', 'Y', 'z', 'Z') >>> d = { k: v for v, k in enumerate(cs) } >>> import random >>> ''.join(sorted(random.sample(cs, 20), key=d.get)) '5aAàÀåBCçËÉíÎLÖøquùx' Regards, Thomas.