String Sort???

Discussion in 'Java' started by ak47, Jun 27, 2005.

  1. ak47

    ak47 Guest

    Hi,
    I would like to sort the string data in following format....

    ArrayList str = new ArrayList();
    str.add("24.1.2.1.1");
    str.add("24.1.1.1" );
    str.add("24.1.2" );
    str.add("24.1" );
    str.add("24.1.1" );
    str.add("24.1.3" );
    str.add("24.1.2.1" );
    str.add("24" );

    java.util.Arrays.sort(str.toArray());

    System.out.println("After sort:" + str.toString());


    Arrays.sort works only with alphabet not in above case?. Any open
    source or helper classes available

    Thanks, Ak
    ak47, Jun 27, 2005
    #1
    1. Advertising

  2. ak47

    Hemal Pandya Guest

    ak47 wrote:
    > Hi,
    > I would like to sort the string data in following format....
    >
    > ArrayList str = new ArrayList();
    > str.add("24.1.2.1.1");
    > str.add("24.1.1.1" );
    > str.add("24.1.2" );
    > str.add("24.1" );
    > str.add("24.1.1" );
    > str.add("24.1.3" );
    > str.add("24.1.2.1" );
    > str.add("24" );
    >
    > java.util.Arrays.sort(str.toArray());
    >
    > System.out.println("After sort:" + str.toString());
    >
    >
    > Arrays.sort works only with alphabet not in above case?.


    Arrays.sort itself does not do any comparing. See the documentation of
    sort(Object[] a). It sorts according to the natural ordering of the
    elements of the array. Since the elements of the array are Strings, the
    comparison is done alphabetically. Think about it -- how else can it
    be?

    > Any open
    > source or helper classes available


    One overloaded version of the sort method accepts a Comparator. It is
    quite simple to implement a Comparator that does the comparison as you
    would like it to be done.

    >
    > Thanks, Ak
    Hemal Pandya, Jun 27, 2005
    #2
    1. Advertising

  3. ak47

    Virgil Green Guest

    ak47 wrote:
    > Hi,
    > I would like to sort the string data in following format....
    >
    > ArrayList str = new ArrayList();
    > str.add("24.1.2.1.1");
    > str.add("24.1.1.1" );
    > str.add("24.1.2" );
    > str.add("24.1" );
    > str.add("24.1.1" );
    > str.add("24.1.3" );
    > str.add("24.1.2.1" );
    > str.add("24" );
    >
    > java.util.Arrays.sort(str.toArray());
    >
    > System.out.println("After sort:" + str.toString());
    >
    >
    > Arrays.sort works only with alphabet not in above case?. Any open
    > source or helper classes available
    >
    > Thanks, Ak


    What is it that you want and what is it that you got when you tried this?

    Look carefully at your code. You never sorted the original ArrayList
    contents. You created a new array that had the same set of Strings and then
    sorted that and immediately discarded it by not retaining any reference to
    it. You then proceed to print the original ArrayList -- which will retain
    the original order.

    I'm not sure what you mean when you say it worked with alphabetic entries.
    --
    Virgil
    Virgil Green, Jun 27, 2005
    #3
  4. ak47

    Dale King Guest

    Hemal Pandya wrote:
    > Arrays.sort itself does not do any comparing. See the documentation of
    > sort(Object[] a). It sorts according to the natural ordering of the
    > elements of the array. Since the elements of the array are Strings, the
    > comparison is done alphabetically. Think about it -- how else can it
    > be?


    Nit pick: The sorting is lexicographic, not alphabetical. Strings are
    sorted by the Unicode code points which does not necessarily correspond
    with natual language alphabets. E.g. the accented characters do not fall
    into the order with the unaccented letters.
    --
    Dale King
    Dale King, Jun 29, 2005
    #4
  5. ak47

    Hemal Pandya Guest

    Dale King wrote:
    > Hemal Pandya wrote:

    [....]
    > > It sorts according to the natural ordering of the
    > > elements of the array. Since the elements of the array are Strings, the
    > > comparison is done alphabetically. Think about it -- how else can it
    > > be?

    >
    > Nit pick: The sorting is lexicographic, not alphabetical. Strings are
    > sorted by the Unicode code points which does not necessarily correspond
    > with natual language alphabets. E.g. the accented characters do not fall
    > into the order with the unaccented letters.


    The phrase "natural ordering" came directly from jdk documentation;
    which -- though the docs don't mention it specifically -- refers to
    whatever algorithm is implemented by `compareTo' method of the Objects
    in the array. It need not be lexicographic and could be even random
    (though I am not sure what will happen if the results are not
    deterministic).

    In case of String you are of course correct. That is why my suggestion
    to create a custom Comparator that does what the OP wants. I am
    interested in knowing if you think that is a good approach.

    > --
    > Dale King


    --
    Hemal Pandya
    Hemal Pandya, Jul 2, 2005
    #5
  6. ak47

    Dale King Guest

    Hemal Pandya wrote:
    > Dale King wrote:
    >
    >>Hemal Pandya wrote:

    >
    > [....]
    >
    >>>It sorts according to the natural ordering of the
    >>>elements of the array. Since the elements of the array are Strings, the
    >>>comparison is done alphabetically. Think about it -- how else can it
    >>>be?

    >>
    >>Nit pick: The sorting is lexicographic, not alphabetical. Strings are
    >>sorted by the Unicode code points which does not necessarily correspond
    >>with natual language alphabets. E.g. the accented characters do not fall
    >>into the order with the unaccented letters.

    >
    >
    > The phrase "natural ordering" came directly from jdk documentation;
    > which -- though the docs don't mention it specifically -- refers to
    > whatever algorithm is implemented by `compareTo' method of the Objects
    > in the array. It need not be lexicographic and could be even random
    > (though I am not sure what will happen if the results are not
    > deterministic).
    >
    > In case of String you are of course correct.


    And that was all I was addressing. The natural ordering of Strings is
    lexicographic.

    In reality the correct way to sort strings is dependent on Locale.
    Different countries sort text differently. E.g. in spanish ch is sorted
    as though it were a single character and in traditional German รค
    (a-umlaut) is sorted as though it were two characters.

    That is the reason for the existence of java.text.Collator and
    java.text.RuleBasedCollator (which I probably should have mentioned before).

    See this page for an example:
    http://www.rgagnon.com/javadetails/java-0343.html

    And to your credit lexicographic is sometimes called alphabetic, but the
    use of the term alphabet there is the mathematical meaning of alphabet
    not the natural meaning that most people would use. I think it is more
    precise to use the term lexicographic. Most poeple won't know what that
    means, but it at least makes it clear to them that it might sort
    differently than they expect.
    --
    Dale King
    Dale King, Jul 2, 2005
    #6
  7. ak47

    Roedy Green Guest

    On 2 Jul 2005 00:16:45 -0700, "Hemal Pandya" <>
    wrote or quoted :

    >In case of String you are of course correct. That is why my suggestion
    >to create a custom Comparator that does what the OP wants. I am
    >interested in knowing if you think that is a good approach.


    see java.text.RuleBasedCollator
    --
    Bush crime family lost/embezzled $3 trillion from Pentagon.
    Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
    http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm

    Canadian Mind Products, Roedy Green.
    See http://mindprod.com/iraq.html photos of Bush's war crimes
    Roedy Green, Jul 6, 2005
    #7
  8. ak47

    Hemal Pandya Guest

    Roedy Green wrote:
    > On 2 Jul 2005 00:16:45 -0700, "Hemal Pandya" <>
    > wrote or quoted :
    >
    > >In case of String you are of course correct. That is why my suggestion
    > >to create a custom Comparator that does what the OP wants. I am
    > >interested in knowing if you think that is a good approach.

    >
    > see java.text.RuleBasedCollator


    Though the documentation says that it "sounds more complicated than it
    is in practice" I am unable to understand how it can be used in this
    context.

    The OP essentially needs a "SectionNumberComparator". Fairly simple to
    implement, and looks almost exactly like a String compare, except that
    individual characters are delimited by "." (as against fixed width in a
    character string) and are of arbitrary size.


    public class SectionNumberComparator{
    int compare(String s1, String s2){
    return compare(toInArray(s1), toInArray(s2));
    }

    int compare(int[] v1, int[] v2) {
    for (int i = 0; i < Math.min(v1.length, v2.length); i++){
    if (v1 != v2){
    return v1 - v2;
    }
    }
    return v1.length - v2.length;
    }

    int[] toInArray(String s) {
    String[] sarray = s.split("\\.");
    int[] retVal = new int[sarray.length];
    for (int i = 0; i < sarray.length; i++){
    retVal = Integer.parseInt(sarray);
    }
    return retVal;
    }

    void check(String s1, String s2) {
    System.out.println('"' + s1 + '"' + " < "
    + '"' + s2 + '"' + " = " + compare(s1, s2));
    }

    public static void main(String args[]){
    SectionNumberComparator c = new SectionNumberComparator();
    c.check("12.1", "1.9");
    c.check("12.1", "12.1.2");
    c.check("1.6", "1.6");
    }
    }
    Hemal Pandya, Jul 6, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. nobody
    Replies:
    0
    Views:
    528
    nobody
    Jun 1, 2004
  2. JerryJ
    Replies:
    11
    Views:
    1,382
    Dave Moore
    Apr 28, 2004
  3. John Black
    Replies:
    6
    Views:
    2,042
    John Harrison
    May 28, 2004
  4. Angus Comber
    Replies:
    7
    Views:
    1,142
    Richard Heathfield
    Feb 5, 2004
  5. Navin
    Replies:
    1
    Views:
    668
    Ken Schaefer
    Sep 9, 2003
Loading...

Share This Page