Page 1 of 1

java string array sorting

Posted: Thu Jul 30, 2009 8:17 pm
by sazzadcsedu
is there any function in java to sort string array in (nlogn) time.
i got a function java.util.Arrays.sort(array);but it does not compare based on their length.it
works only on same length string.
so can anyone help me to know about whether/not any function exist. ???

Re: java string array sorting

Posted: Fri Jul 31, 2009 8:04 am
by mf
Please, don't post text in funny colors, and capitalize the first letter of each sentence.


java.utils.Arrays.sort(Object[]) uses mergesort, and so runs in Theta(n log n) time (assuming that each comparison takes O(1) time).
sazzadcsedu wrote:i got a function java.util.Arrays.sort(array);but it does not compare based on their length.it works only on same length string.
What do you mean? It compares objects using their compareTo() function of java.lang.Comparable interface that they must implement. String's default comparison function is lexicographic, it works for strings of any size. If you don't like it, you can override it.

Re: java string array sorting

Posted: Fri Jul 31, 2009 7:38 pm
by sazzadcsedu
Ok.
But compareTo function works on only same length string.
If String s[]={"aac","ab","abc","asd","a"};
Then it return :
a
aac
ab
abc
asd

Which is not based on length .so if i want to get like:
a
ab
aac
abc
asd
Then, is any function available??????
Or can you give me details/defination of (compareTo && java.utils.Arrays.sort(Object[])) function so that i can modify them.

Re: java string array sorting

Posted: Sat Aug 01, 2009 12:45 am
by mf
Implement your comparison function using java.util.Comparator interface and pass an instance of that to java.util.Arrays.sort():

Code: Select all

import java.util.*;

class MyComparator implements Comparator<String> {
    public int compare(String s, String t) {
        if (s.length() != t.length())
            return s.length() - t.length();
        else
            return s.compareTo(t);
    }
}

public class Main {
    public static void main(String[] args) {
        Arrays.sort(args, new MyComparator());
        for (String s : args)
            System.out.println(s);
    }
}
or even:

Code: Select all

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Arrays.sort(args, new Comparator<String>() {
            public int compare(String s, String t) {
                if (s.length() != t.length())
                    return s.length() - t.length();
                else
                    return s.compareTo(t);
            }
        });

        for (String s : args)
            System.out.println(s);
    }
}
Or can you give me details/defination of (compareTo && java.utils.Arrays.sort(Object[])) function so that i can modify them.
http://hg.openjdk.java.net/jdk6/jdk6-ga ... rrays.java

Re: java string array sorting

Posted: Thu Aug 06, 2009 10:49 pm
by sazzadcsedu
Thanx mf for your help.Sorry for late reply.
Can you give me some critical i/o for 10364(squre)???

Re: java string array sorting

Posted: Fri Aug 07, 2009 2:25 pm
by mf
sazzadcsedu wrote:Can you give me some critical i/o for 10364(squre)???
No, why on earth would I have it?!

But that problem seems to be from Waterloo programing contests. They always publish their data and solutions, google them.