java string array sorting

Write here if you have problems with your Java source code

Moderator: Board moderators

Post Reply
sazzadcsedu
Experienced poster
Posts: 136
Joined: Sat Nov 29, 2008 8:01 am
Location: narayangong,bangladesh.
Contact:

java string array sorting

Post 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. ???
Life is more complicated than algorithm.
http://felix-halim.net/uva/hunting.php?id=32359
For Hints: http://salimsazzad.wordpress.com
mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:

Re: java string array sorting

Post 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.
sazzadcsedu
Experienced poster
Posts: 136
Joined: Sat Nov 29, 2008 8:01 am
Location: narayangong,bangladesh.
Contact:

Re: java string array sorting

Post 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.
Life is more complicated than algorithm.
http://felix-halim.net/uva/hunting.php?id=32359
For Hints: http://salimsazzad.wordpress.com
mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:

Re: java string array sorting

Post 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
sazzadcsedu
Experienced poster
Posts: 136
Joined: Sat Nov 29, 2008 8:01 am
Location: narayangong,bangladesh.
Contact:

Re: java string array sorting

Post by sazzadcsedu »

Thanx mf for your help.Sorry for late reply.
Can you give me some critical i/o for 10364(squre)???
Life is more complicated than algorithm.
http://felix-halim.net/uva/hunting.php?id=32359
For Hints: http://salimsazzad.wordpress.com
mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:

Re: java string array sorting

Post 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.
Post Reply

Return to “Java”