Alphabetical sorts




I have several applications where I want to sort lists in alphabetical order. Most examples of sorting usually sort on the ord() order of the character set as an approximation. But that is not always what you want.

The solution of converting everything to lowercase or uppercase is closer, but it would be nice if capitalized words come before lowercase words of the same spellings. And I suspect ord() order may not be correct for some character sets.

So I'm wandering what others have done and weather there is something in the standard library I haven't found for doing this.

Below is my current way of doing it, but I think it can probably be improved quite a bit.

This partial solution also allows ignoring leading characters such as spaces, tabs, and underscores by specifying what not to ignore. So '__ABC__' will be next to 'ABC'. But this aspect isn't my main concern.

Maybe some sort of alphabetical order string could be easily referenced for various alphabets instead of having to create them manually?

Also it would be nice if strings with multiple words were ordered correctly.


Cheers,
_Ron



def stripto(s, goodchrs):
""" Removes leading and trailing characters from string s
which are not in the string goodchrs.
"""
badchrs = set(s)
for c in goodchrs:
if c in badchrs:
badchrs.remove(c)
badchrs = ''.join(badchrs)
return s.strip(badchrs)


def alpha_sorted(seq):
""" Sort a list of strings in 123AaBbCc... order.
"""
order = ( '0123456789AaBbCcDdEeFfGgHhIiJjKkLlMmNn'
'OoPpQqRrSsTtUuVvWwXxYyZz' )
def chr_index(value, sortorder):
""" Make a sortable numeric list
"""
result = []
for c in stripto(value, order):
cindex = sortorder.find(c)
if cindex == -1:
cindex = len(sortorder)+ord(c)
result.append(cindex)
return result

deco = [(chr_index(a, order), a) for a in seq]
deco.sort()
return list(x[1] for x in deco)
.



Relevant Pages

  • Re: How to add a string to a big file in csharp !
    ... "zjut" wrote in message ... > I want to add a string to the file and the file is sort by letter! ... Your file is in alphabetical order ...
    (microsoft.public.dotnet.languages.csharp)
  • sorting a string collection
    ... Can anybody please tell me if it is possible to sort a string collection ... into alphabetical order. ... Please can somebody let me know the best way to implement a sort method -- ...
    (microsoft.public.dotnet.framework)
  • Re: sorting a string collection
    ... Can anybody please tell me if it is possible to sort a string ... into alphabetical order. ... "System.Collections.Specialized" namespace, however there does not ...
    (microsoft.public.dotnet.framework)
  • Re: VB6 LISTBOX problem
    ... Have a look at the code below, can you do a sort that is faster ... There are of course all sorts of ways of sorting stuff, especially stuf that is "linked together" as in your UDTs, but to prove the following code sorts it in exactly the same way that your own original code does so, by concatenating all three items of each element into a composite string exactly as you are already doing, but instead of dumping those composite strings into a ListBox it dumps them into a VB string array. ... Private Type SAFEARRAY1D ... Dim StPtr As Long, VAs String, pVAs Long ...
    (microsoft.public.vb.general.discussion)
  • Re: need help sorting text by trailing Numbers Value
    ... You can try the following VBA code and see if it works to give you a sort string you can use. ... Dim strReturn As String ... Then in the next step how could i move all of the trailing Numerical charachters to a new field? ...
    (microsoft.public.access.reports)