String Sort???

A

ak47

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
 
H

Hemal Pandya

ak47 said:
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.
 
V

Virgil Green

ak47 said:
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.
 
D

Dale King

Hemal said:
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.
 
H

Hemal Pandya

Dale said:
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.
 
D

Dale King

Hemal said:
Dale said:
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.
 
R

Roedy Green

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
 
H

Hemal Pandya

Roedy said:
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");
}
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,526
Members
44,997
Latest member
mileyka

Latest Threads

Top