Re: Sorting atoms in a case-insensitive manner?

From: Jan Wielemaker (jan_at_ct.xs4all.nl)
Date: 11/29/03

  • Next message: Bart Demoen: "Re: Sorting atoms in a case-insensitive manner?"
    Date: 29 Nov 2003 14:26:59 GMT
    
    

    In article <pan.2003.11.29.11.28.11.885516@in.signature>, seguso wrote:
    >
    > Is there any easy way to sort a list of atoms in a case-insensitive manner?
    >
    > predsort(compare, List, SortedList).
    >
    > sorts it like that:
    >
    > Aaaa
    > BBbbb
    > aaaa
    > bbbb
    >
    > whereas I need
    >
    > Aaaa
    > aaaa
    > BBbbb
    > bbbb
    >
    > Thanks for your kind help.

    Not really. As you are using SWI-Prolog anyway, the fastest definition
    is:

    case_insensitive_sort(Atoms, Sorted) :-
            key_case(Atoms, Keyed),
            keysort(Keyed, Sorted0),
            unkey(Sorted0, Sorted).

    key_case([], []).
    key_case([H|T0], [L-H|T]) :-
            downcase_atom(H, L),
            key_case(T0, T).

    unkey([], []).
    unkey([_-T0], T) :-
            unkey(T0, T).

    (not tested).

    I've considered adding a built-in as this operation is quite common in
    some application domains and a built-in avoids the creation of a large
    number of garbage atoms.

            --- Jan


  • Next message: Bart Demoen: "Re: Sorting atoms in a case-insensitive manner?"

    Relevant Pages

    • Sorting atoms in a case-insensitive manner?
      ... Is there any easy way to sort a list of atoms in a case-insensitive manner? ... Please remove the uppercase letters "S,P,A,M": ...
      (comp.lang.prolog)
    • Re: Everything is made up of atoms, correct ?
      ... Would we see nothing but atoms all around us? ... IOW what sort of resolution would be needed? ... Because things happening on that level don't matter much except in ... at larger-than-atomic scales because that's where we live. ...
      (sci.physics)
    • Re: Using hashes to sort number sequences
      ... For each structure there are six atoms. ... one can compare the coordination sequences (the number ... two permutations of the same atoms are equal (no matter how you sort). ... not sure about the effect of key length on Perl's string sort. ...
      (comp.lang.perl.misc)
    • Re: Bad Science - Good Fiction
      ... :: They said the same thing about atoms. ... Who said that about atoms and nuclei? ... Of course, they didn't really say "they can never be found free in nature", ... sort of thing you'd like if you like that sort of thing. ...
      (rec.arts.sf.written)