sorting Arraylist

i have one ArrayList, that ArrayList is see below,arrlist={A,A1,A2,A3,B.B1,B2,B3,C,C1,C2,.......}Like that.i want to Change that Arraylist like that below,arrlist={A,B,C,A1,B1,C1,A2,B2.C2.......}PLEASE KINDLY HELP ME.
[252 byte] By [arun_anand_1985a] at [2007-11-27 3:42:13]
# 1
Collections.sort()
OleVVa at 2007-7-12 8:45:48 > top of Java-index,Core,Core APIs...
# 2

To expand on the previous answer a bit, just using Collections.sort() isn't enough.

I assume that your ArrayList contains Strings. In this case, just doing Collections.sort(arrlist) won't produce the required ordering - in fact it won't change it at all, because the list is already sorted according to the rules of ordering Strings.

To achieve this "custom" ordering you'll need to write a Comparator for Strings (i.e. a class that implements Comparator<String>) that knows how to sort two Strings according to your rules. You then call Collections.sort(arrlist, comparator).

Alternatively, if each element in arrlist is of a type that you've written (one your of your own classes), then you can either (1) implement Comparable<String> in that class, and just call the 1-argument Collections.sort() method or (2) write a Comparator class, as above.

Both ways of doing it achieve the same thing, but (1) puts the ordering rules in the class (and you could then regard it as the "primary" or "preferred" way of ordering objects of that class), whereas (2) keeps the ordering rules separate from the class itself.

RichFearna at 2007-7-12 8:45:48 > top of Java-index,Core,Core APIs...
# 3
Thanks, RichFearn, for elaborating. You are right, of course. I would use the Compator approach if the sort order required here was not the sort order I would expect for ordering another time in another context.
OleVVa at 2007-7-12 8:45:48 > top of Java-index,Core,Core APIs...
# 4

Hi RichFeam,

I thought of getting the arraylist without duplicates and ordered using TreeSet.

List list = new ArrayList();

Just we pass list object into the TreeSet.

Set s = new TreeSet(list);

Can anyone tell is ithis good solution for this problem.

Thanks

Maruthi

mars@sayampua at 2007-7-12 8:45:48 > top of Java-index,Core,Core APIs...
# 5

No, it's not a good solution - that just deals with sorting in a different way (more complicated than is actually necessary).

It doesn't address the problem of how you define the rules for ordering the elements.

Collections.sort is the simplest way to sort a list. But you need to define the ordering rules.

RichFearna at 2007-7-12 8:45:48 > top of Java-index,Core,Core APIs...
# 6
In addition, adding (the contents of) a list to a set removes any duplicates that may appear in the list.
Herko_ter_Horsta at 2007-7-12 8:45:48 > top of Java-index,Core,Core APIs...
# 7

Here's a quick implementation of Rich's suggestions. It'll sort your list, but won't handle any identifiers that are > 2 digits (for example, C13 or C1A).

import java.util.ArrayList;

import java.util.Collections;

import java.util.Comparator;

class CustomSort implements Comparator<String> {

public int compare(String s1, String s2) {

if( s1.length() < s2.length() ) {

return -1;

}

if( s1.length() > s2.length() ) {

return 1;

}

if( s1.length() == s2.length() ) {

if( s1.length() == 1 ) { // compare first char

return s1.compareTo(s2);

}

else { // s1.length() > 1

return s1.substring(0,2).compareTo(s2.substring(0,2));

}

}

return -1; // never returned

}

}

public class cust_sort {

public static void main(String[] args) {

ArrayList<String> arraylist = new ArrayList<String>();

arraylist.add("A");

arraylist.add("A1");

arraylist.add("A2");

arraylist.add("A3");

arraylist.add("B");

arraylist.add("B1");

arraylist.add("B2");

arraylist.add("B3");

arraylist.add("C");

arraylist.add("C1");

arraylist.add("C2");

System.out.println("custom sort");

Collections.sort(arraylist, new CustomSort());

for( String s: arraylist ) {

System.out.print( s+ "," );

}

}

}

C_Walkera at 2007-7-12 8:45:48 > top of Java-index,Core,Core APIs...
# 8

What's the output of your program? From reading quickly through it, I would expect it to put A2 before B1, contrary to the requirements.

Ole

PS It's a matter of taste; I would prefer the list of if statements written this way:

if( s1.length() < s2.length() ) {

return -1;

}

else if( s1.length() > s2.length() ) {

return 1;

}

else { // s1.length() == s2.length()

if( s1.length() == 1 ) { // compare first char

return s1.compareTo(s2);

}

else { // s1.length() > 1

return s1.substring(0,2).compareTo(s2.substring(0,2));

}

}

OleVVa at 2007-7-12 8:45:48 > top of Java-index,Core,Core APIs...
# 9

> PS It's a matter of taste; I would prefer the list of

> if statements written this way:

Few things annoy me more than 'return' followed by a redundant 'else', but one of them is poorly written comparison routines. Try this:

int diff = s1.length()-s2.length();

if (diff != 0)

return diff;

return s1.compareTo(s2);

And the premature first-char comparison optimization will have zero or negative effect as written. Counterproof welcome.

ejpa at 2007-7-12 8:45:48 > top of Java-index,Core,Core APIs...
# 10

> Few things annoy me more than 'return' followed by a

> redundant 'else', but one of them is poorly written

> comparison routines. Try this:

Which, I guess, comfirms that it's a matter of taste. I, on my part, have come to like braces around the inner statements of an if statement. I have fellow programmers who object to a return statement placed anywhere but at the bottom of a method.

The brevity of your code is, of course, admirable.

OleVVa at 2007-7-12 8:45:48 > top of Java-index,Core,Core APIs...
# 11

this code looks like a2 preceeds b1, which is against the specification...

For an alternative solution, maybe...

int diff = s1.length() - s2.length();

if(diff != 0)

return diff;

int s1i = Integer.parseInt(s1.substring(1));

int s2i = Integer.parseInt(s2.substring(1));

diff = s1i - s2i;

if(diff != 0)

return diff;

return s1.charAt(0) - s2.charAt(0);

I didn't test the code, but I think that should work, given the specification:

sortedList = { A, B, C, A1, B1, C1, A2, B2, C2 }

Also, on the topic of "reduntant else"s.... which is more optimized? to have:

if( x > 1) return 1;

else if ( x < 1) return foo(x);

else return 0;

or

if(x > 1) return 1;

if(x < 1) return foo(x);

return 0;

as far as runtime is concerned?

Message was edited by:

sukuriant

sukurianta at 2007-7-12 8:45:48 > top of Java-index,Core,Core APIs...
# 12
They're the same at runtime, and the compiler is probably smart enough to remove the unreachable code associated with the 'else' so it probably doesn't even cost any space. It just irritates me to see return/else: it is pointless.
ejpa at 2007-7-12 8:45:48 > top of Java-index,Core,Core APIs...