Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'parameter': 0.07; "'w',": 0.09; 'to:addr:comp.lang.python': 0.09; 'cc:addr:python-list': 0.10; '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; 'character:': 0.16; 'mon,': 0.16; 'wrote:': 0.17; '>>>': 0.18; 'appropriate': 0.20; 'sort': 0.21; 'import': 0.21; 'cc:2**0': 0.23; 'monday,': 0.23; 'random': 0.24; 'cc:no real name:2**0': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header :User-Agent:1': 0.26; 'skip:[ 10': 0.26; "doesn't": 0.28; 'words': 0.29; "skip:' 10": 0.30; 'december': 0.32; "skip:' 20": 0.32; 'received:google.com': 0.34; 'mapping': 0.35; 'received:209.85': 0.35; 'option': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'thomas': 0.62; 'more': 0.63; '8bit%:50': 0.65; "'2',": 0.84; "'3',": 0.84; 'order:': 0.84 Newsgroups: comp.lang.python Date: Mon, 24 Dec 2012 08:34:30 -0800 (PST) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=87.212.130.235; posting-account=_qTNOQkAAAAIEjEY1tV1q-dc2n_jsg5V References: <40d108ec-b019-4829-a969-c8ef513866f1@googlegroups.com> User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-IP: 87.212.130.235 MIME-Version: 1.0 Subject: Re: Custom alphabetical sort From: Pander Musubi To: comp.lang.python@googlegroups.com Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org 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: , Message-ID: Lines: 66 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1356366879 news.xs4all.nl 6990 [2001:888:2000:d::a6]:40582 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:35468 On Monday, December 24, 2012 5:11:03 PM UTC+1, Thomas Bach wrote: > On Mon, Dec 24, 2012 at 07:32:56AM -0800, Pander Musubi wrote: >=20 > > I would like to sort according to this order: >=20 > >=20 >=20 > > (' ', '.', '\'', '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',= 'a', 'A', '=E4', '=C4', '=E1', '=C1', '=E2', '=C2', '=E0', '=C0', '=E5', '= =C5', 'b', 'B', 'c', 'C', '=E7', '=C7', 'd', 'D', 'e', 'E', '=EB', '=CB', '= =E9', '=C9', '=EA', '=CA', '=E8', '=C8', 'f', 'F', 'g', 'G', 'h', 'H', 'i',= 'I', '=EF', '=CF', '=ED', '=CD', '=EE', '=CE', '=EC', '=CC', 'j', 'J', 'k'= , 'K', 'l', 'L', 'm', 'M', 'n', '=F1', 'N', '=D1', 'o', 'O', '=F6', '=D6', = '=F3', '=D3', '=F4', '=D4', '=F2', '=D2', '=F8', '=D8', 'p', 'P', 'q', 'Q',= 'r', 'R', 's', 'S', 't', 'T', 'u', 'U', '=FC', '=DC', '=FA', '=DA', '=FB',= '=DB', '=F9', '=D9', 'v', 'V', 'w', 'W', 'x', 'X', 'y', 'Y', 'z', 'Z') >=20 > >=20 >=20 >=20 >=20 > One option is to use sorted's key parameter with an appropriate >=20 > mapping in a dictionary: >=20 >=20 >=20 > >>> cs =3D (' ', '.', '\'', '-', '0', '1', '2', '3', '4', '5', '6', '7', = '8', '9', 'a', 'A', '=E4', '=C4', '=E1', '=C1', '=E2', '=C2', '=E0', '=C0',= '=E5', '=C5', 'b', 'B', 'c', 'C', '=E7', '=C7', 'd', 'D', 'e', 'E', '=EB',= '=CB', '=E9', '=C9', '=EA', '=CA', '=E8', '=C8', 'f', 'F', 'g', 'G', 'h', = 'H', 'i', 'I', '=EF', '=CF', '=ED', '=CD', '=EE', '=CE', '=EC', '=CC', 'j',= 'J', 'k', 'K', 'l', 'L', 'm', 'M', 'n', '=F1', 'N', '=D1', 'o', 'O', '=F6'= , '=D6', '=F3', '=D3', '=F4', '=D4', '=F2', '=D2', '=F8', '=D8', 'p', 'P', = 'q', 'Q', 'r', 'R', 's', 'S', 't', 'T', 'u', 'U', '=FC', '=DC', '=FA', '=DA= ', '=FB', '=DB', '=F9', '=D9', 'v', 'V', 'w', 'W', 'x', 'X', 'y', 'Y', 'z',= 'Z') >=20 >=20 >=20 > >>> d =3D { k: v for v, k in enumerate(cs) } >=20 >=20 >=20 > >>> import random >=20 >=20 >=20 > >>> ''.join(sorted(random.sample(cs, 20), key=3Dd.get)) >=20 > '5aA=E0=C0=E5BC=E7=CB=C9=ED=CEL=D6=F8qu=F9x' This doesn't work for words with more than one character: >>> test=3D('=F8asdf', '=E1=E1', 'aa', 'a123','=E11234', 'Aaa', ) >>> sorted(test, key=3Dd.get) ['\xc3\xb8asdf', '\xc3\xa1\xc3\xa1', 'aa', 'a123', '\xc3\xa11234', 'Aaa'] >=20 >=20 >=20 > Regards, >=20 > Thomas.