>> Its really important.
Maybe you could have considered 500pts ;°)
Main Topics
Browse All TopicsHi,
Below is the class which sorts the list of strings and integers. I passed the arguments as 115R 1125R 147C 37R 57R 37C and it shows the result as 1125R 115R 147C 37C 37R 57R. But the problem is I want the output as 37C 37R 57R 115R 147C 1125R. Can anyone help me with this. Its really important.
import java.util.*;
public class SortArrayList
{
public static void main(String s[])
{
Collections col;
List l = sort(s);
System.out.println("\nStri
for(int i = 0; i < s.length; i++)
{
System.out.println((String
}
int ints[] = {
11, 2, -22, 401, 6
};
Integer in[] = new Integer[ints.length];
for(int i = 0; i < in.length; i++)
{
in[i] = new Integer(ints[i]);
}
l = sort(in);
System.out.println("\nInte
for(int i = 0; i < in.length; i++)
{
System.out.println((Intege
}
}
public static List sort(Object o[])
{
ArrayList al = new ArrayList();
for(int i = 0; i < o.length; i++)
al.add(i, o[i]);
List list = Collections.synchronizedLi
Collections.sort(list);
return list;
}
}
Thanks.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Correction:
Collections.sort(list, new Comparator() {
public int compare(Object o1, Object o2) {
String s1 = (String)o1;
String s2 = (String)o2;
String integer1[] = s1.split("[^0-9]"); // <<<<< changed
String integer2[] = s2.split("[^0-9]"); // <<<<< changed
String chars1[] = s1.split("[0-9]+"); // <<<<< changed
String chars2[] = s2.split("[0-9]+"); // <<<<< changed
Integer i1 = new Integer( Integer.parseInt(integer1[
Integer i2 = new Integer( Integer.parseInt(integer2[
if (i1.equals(i2)
return chars1[1].compareTo(chars2
else
return i1.compareTo(i2);
}
});
zzynx is 100% correct that you should use a comparator, but you might find a simpler impelementation more readable.
I'm also not a big fan of anonymous inner class (they clutter the code something awful IMO).
Something like this would work just as well:
Collections.sort(list, new SpecialComarator());
Class SpecialComparator
implements Comparator
{
public int compare(Object o1, Object o2)
{
String s1 = (String)o1;
String s2 = (String)o2;
int offset1 = findCharOffset(s1);
int offset2 = findCharOffset(s2);
int v1 = Integer.parseInt(s1.substr
int v2 = Integer.parseInt(s2.substr
int difference = v1 - v2;
if (difference != 0)
{
return difference;
}
return s1.substring(offset1).comp
}
private int findCharOffset(String value)
{
integer result = 0;
while (result < value.length() && Character.isDigit(value.ch
{
result++;
}
return result;
}
}
>> I'm also not a big fan of anonymous inner class
In this case I find an inner class is on it's place: It's here that you need the sorting; it's here that you find the Comparator class.
But, that's a matter of taste of course ;°)
>>...but you might find a simpler impelementation more readable.
Well, it's a matter of being/making yourself familiar with regular expressions.
In the past I also wasn't a big fan of them.
But once you discover their power...
Are you sure?
For me it just works:
List list = new ArrayList();
list.add("1125R");
list.add("115R");
list.add("147C");
list.add("37C");
list.add("37R");
list.add("57R");
Collections.sort(list, new Comparator() {
public int compare(Object o1, Object o2) {
String s1 = (String)o1;
String s2 = (String)o2;
String integer1[] = s1.split("[^0-9]"); // <<<<< changed
String integer2[] = s2.split("[^0-9]"); // <<<<< changed
String chars1[] = s1.split("[0-9]+"); // <<<<< changed
String chars2[] = s2.split("[0-9]+"); // <<<<< changed
Integer i1 = new Integer( Integer.parseInt(integer1[
Integer i2 = new Integer( Integer.parseInt(integer2[
if (i1.equals(i2))
return chars1[1].compareTo(chars2
else
return i1.compareTo(i2);
}});
System.out.println("\nStri
for(int i = 0; i < list.size(); i++) {
System.out.println((String
}
Output:
Strings sorted List ...
37C
37R
57R
115R
147C
1125R
>>>...but you might find a simpler impelementation more readable.
> Well, it's a matter of being/making yourself familiar with regular expressions.
> In the past I also wasn't a big fan of them.
> But once you discover their power...
Sure, regular expressions are powerful things and I know full well HOW to use them. I'm not conviced that they are the most appropriate solution in this case - they're TOO powerful and unnecessarily complicate an essentially simple problem.
An of course, that too is a matter of taste. I'm a card carrying member of "Do The Simplest Thing That Could Possibly Work".
Actually thinking about it more clearly, something like this would also work:
Class SpecialComparator
implements Comparator
{
public int compare(Object o1, Object o2)
{
String s1 = (String)o1;
String s2 = (String)o2;
int v1 = Integer.parseInt(s1.substr
int v2 = Integer.parseInt(s2.substr
int difference = v1 - v2;
if (difference != 0)
{
return difference;
}
return Character.getNumericValue(
Character.getNumericValue(
}
Hi zzynx,
Here is the exact code which I have. Please help me with this. Here is the arguments which I passed while executing 115R 1125R 147C 37R 57R 37C and I am getting the output as 1125R 115R 147C 37C 37R 57R. Below is the code:
import java.util.*;
public class SortObjects
{
public static void main(String s[])
{
Collections col;
List l = sort(s);
System.out.println("\nStri
for(int i = 0; i < s.length; i++)
{
System.out.println((String
}
int ints[] = {
11, 2, -22, 401, 6
};
Integer in[] = new Integer[ints.length];
for(int i = 0; i < in.length; i++)
{
in[i] = new Integer(ints[i]);
}
l = sort(in);
System.out.println("\nInte
for(int i = 0; i < in.length; i++)
{
System.out.println((Intege
}
}
public static List sort(Object o[])
{
ArrayList al = new ArrayList();
for(int i = 0; i < o.length; i++)
al.add(i, o[i]);
List list = Collections.synchronizedLi
Collections.sort(list, new Comparator()
{
public int compare(Object o1, Object o2)
{
String s1 = (String)o1;
String s2 = (String)o2;
int v1 = Integer.parseInt(s1.substr
int v2 = Integer.parseInt(s2.substr
int difference = v1 - v2;
if (difference != 0)
{
return difference;
}
return Character.getNumericValue(
Character.getNumericValue(
}
});
//Collections.sort(list , new SpecialComparator());
System.out.println("\nStri
for(int i = 0; i < list.size(); i++)
System.out.println((String
return list;
}
}
Thanks in advance.
hi,
I have tried this code too.
/*
* Created on Dec 10, 2004
*
* @author z688240
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
import java.util.*;
public class SortObjects
{
public static void main(String s[])
{
Collections col;
List l = sort(s);
System.out.println("\nStri
for(int i = 0; i < s.length; i++)
{
System.out.println((String
}
int ints[] = {
11, 2, -22, 401, 6
};
Integer in[] = new Integer[ints.length];
for(int i = 0; i < in.length; i++)
{
in[i] = new Integer(ints[i]);
}
l = sort(in);
System.out.println("\nInte
for(int i = 0; i < in.length; i++)
{
System.out.println((Intege
}
}
public static List sort(Object o[])
{
ArrayList al = new ArrayList();
for(int i = 0; i < o.length; i++)
al.add(i, o[i]);
List list = Collections.synchronizedLi
Collections.sort(list, new Comparator()
{
public int compare(Object o1, Object o2)
{
String s1 = (String)o1;
String s2 = (String)o2;
String integer1[] = s1.split("[^0-9]"); // <<<<< changed
String integer2[] = s2.split("[^0-9]"); // <<<<< changed
String chars1[] = s1.split("[0-9]+"); // <<<<< changed
String chars2[] = s2.split("[0-9]+"); // <<<<< changed
Integer i1 = new Integer( Integer.parseInt(integer1[
Integer i2 = new Integer( Integer.parseInt(integer2[
if (i1.equals(i2))
return chars1[1].compareTo(chars2
}
});
System.out.println("\nStri
for(int i = 0; i < list.size(); i++)
System.out.println((String
return list;
}
}
Hi sunny,
this should work (I felt free to remove all obsolete stuff):
/*
* SortObjects.java
*
* Created on 13 december 2004, 8:47
*/
import java.util.*;
public class SortObjects
{
public static void main(String s[])
{
// The input is a String array. Make that an ArrayList:
List inputList = new ArrayList(Arrays.asList(s)
List l = sort(inputList);
System.out.println("\nStri
for(int i = 0; i < l.size(); i++)
System.out.println((String
}
public static List sort(List list) {
Collections.sort(list, new Comparator() {
public int compare(Object o1, Object o2) {
String s1 = (String)o1;
String s2 = (String)o2;
String integer1[] = s1.split("[^0-9]"); // <<<<< changed
String integer2[] = s2.split("[^0-9]"); // <<<<< changed
String chars1[] = s1.split("[0-9]+"); // <<<<< changed
String chars2[] = s2.split("[0-9]+"); // <<<<< changed
Integer i1 = new Integer( Integer.parseInt(integer1[
Integer i2 = new Integer( Integer.parseInt(integer2[
if (i1.equals(i2))
return chars1[1].compareTo(chars2
else
return i1.compareTo(i2);
}
});
return list;
}
}
But in fact, I wouldn't even make that a separate function:
/*
* SortObjects.java
*
* Created on 13 december 2004, 8:47
*/
import java.util.*;
public class SortObjects
{
public static void main(String s[])
{
// The input is a String array. Make that an ArrayList:
List inputList = new ArrayList(Arrays.asList(s)
Collections.sort(inputList
public int compare(Object o1, Object o2) {
String s1 = (String)o1;
String s2 = (String)o2;
String integer1[] = s1.split("[^0-9]");
String integer2[] = s2.split("[^0-9]");
String chars1[] = s1.split("[0-9]+");
String chars2[] = s2.split("[0-9]+");
Integer i1 = new Integer( Integer.parseInt(integer1[
Integer i2 = new Integer( Integer.parseInt(integer2[
if (i1.equals(i2))
return chars1[1].compareTo(chars2
else
return i1.compareTo(i2);
}
});
System.out.println("\nStri
for(int i = 0; i < inputList.size(); i++)
System.out.println((String
}
}
Hello zzynx,
I really appreciate the response but I don't know, I already tried the code 2 days back without writing different function and different possibilities. But it doesn't work for me. The arguments which I passed are 115R 1125R 147C 37R 57R 37C and u seem to get the output as expected but I get different output which is different from what I want which is weird. The output which I got is :
Strings sorted List ...
1125R
115R
147C
37C
37R
57R
Can you send me the code which u executed on ur side.
Thanks
>>Can you send me the code which u executed on ur side
Sure. (But in fact I already did)
This is my SortObject.java file:
import java.util.*;
public class SortObjects
{
public static void main(String s[])
{
String t[] = { "115R", "1125R", "147C", "37R", "57R", "37C" }; // This is to simulate the user input
// The input is a String array. Make that an ArrayList:
List inputList = new ArrayList(Arrays.asList(t)
List l = sort(inputList);
System.out.println("\nStri
for(int i = 0; i < l.size(); i++)
System.out.println((String
}
public static List sort(List list) {
Collections.sort(list, new Comparator() {
public int compare(Object o1, Object o2) {
String s1 = (String)o1;
String s2 = (String)o2;
String integer1[] = s1.split("[^0-9]"); // <<<<< changed
String integer2[] = s2.split("[^0-9]"); // <<<<< changed
String chars1[] = s1.split("[0-9]+"); // <<<<< changed
String chars2[] = s2.split("[0-9]+"); // <<<<< changed
Integer i1 = new Integer( Integer.parseInt(integer1[
Integer i2 = new Integer( Integer.parseInt(integer2[
if (i1.equals(i2))
return chars1[1].compareTo(chars2
else
return i1.compareTo(i2);
}
});
return list;
}
}
The output of this program is:
Strings sorted List ...
37C
37R
57R
115R
147C
1125R
Hi zzynx,
I have some problem with the code, I am getting an exception as below:
java.lang.ArrayIndexOutOfB
at SortObjects$1.compare(Sort
at java.util.Arrays.mergeSort
at java.util.Arrays.sort(Arra
at java.util.Collections.sort
at SortObjects.main(SortObjec
Exception in thread "main"
and the code is as below:
import java.util.*;
public class SortObjects
{
public static void main(String s[])
{
String t[] = { "NITT", "A123" };
List inputList = new ArrayList(Arrays.asList(t)
//ArrayList inputList = new ArrayList(Arrays.asList(s)
if(inputList != null && inputList.size() > 0)
{
Collections.sort(inputList
{
public int compare(Object o1, Object o2)
{
String s1 = (String)o1;
String s2 = (String)o2;
String integer1[] = s1.split("[^0-9]");
String integer2[] = s2.split("[^0-9]");
String chars1[] = s1.split("[0-9]+");
String chars2[] = s2.split("[0-9]+");
Integer i1 = new Integer(Integer.parseInt(i
Integer i2 = new Integer(Integer.parseInt(i
if (i1.equals(i2))
return chars1[1].compareTo(chars2
else
return i1.compareTo(i2);
}
});
}
System.out.println("\nStri
for(int i = 0; i < inputList.size(); i++)
System.out.println((String
}
}
Please help me with this.
Thanks in advance.
Sunny
Hi zzynx, I am having some problem with this program. I tried so many ways for fixing this but no luck. I need help with this problem. Below is the code:
import java.util.*;
public class SortObjects
{
public static ArrayList getSortedList(ArrayList sortableList)
{
if(sortableList != null)
{
if(sortableList.size() == 0)
return sortableList;
}
else
return sortableList;
List inputList = sortableList;
Collections.sort(inputList
{
public int compare(Object o1, Object o2)
{
String s1 = (String)o1;
String s2 = (String)o2;
System.out.println("$$$$$$
String integer1[] = s1.split("[^0-9]");
String integer2[] = s2.split("[^0-9]");
String chars1[] = s1.split("[0-9]+");
String chars2[] = s2.split("[0-9]+");
Integer i1 = null;
Integer i2 = null;
String str1 = null;
String str2 = null;
if(integer1.length > 0 && integer2.length > 0)
{
try
{
i1 = new Integer(Integer.parseInt(i
i2 = new Integer(Integer.parseInt(i
}
catch(NumberFormatExceptio
{
if(chars1.length > 0 && chars2.length > 0)
{
str1 = chars1[0];
str2 = chars2[0];
}
}
}
else if(chars1.length > 0 && chars2.length > 0)
{
str1 = chars1[0];
str2 = chars2[0];
}
System.out.println("i1 :"+i1.intValue()+" i2 :"+i2.intValue());
if(i1 != null && i2 != null)
{
System.out.println("%%%%%%
if (i1.equals(i2))
{
System.out.println("%%%%%%
System.out.println("%%%%%%
if(chars1[1] != null && chars2[1] != null)
return chars1[1].compareTo(chars2
else
return 0;
}
else
{
System.out.println("%%%%%%
return i1.compareTo(i2);
}
}
else if(str1 != null && str2 != null)
{
if(str1.equals(str2))
return chars1[1].compareTo(chars2
else
return str1.compareTo(str2);
}
return 0;
}
});
return (ArrayList)inputList;
}
public static void main(String[] args)
{
SortObjects sTest = new SortObjects();
ArrayList list = new ArrayList();
list.add("011");
list.add("011A");
list.add("011AB");
list.add("011AA");
ArrayList sList = getSortedList(list);
for(int i=0; i < sList.size(); i++)
{
System.out.println("i value :"+i+" data :"+(String)sList.get(1));
}
}
}
Exception which I am getting is :
$$$$$$$$$s1 : 011 s2:011A
i1 :11 i2 :11
%%%%%%%%
%%%%%%%%1111 chars1[1] :null
%%%%%%%%% chars2[1] :null
java.lang.ArrayIndexOutOfB
at SortObjects$1.compare(Sort
at java.util.Arrays.mergeSort
at java.util.Arrays.sort(Arra
at java.util.Collections.sort
at SortObjects.getSortedList(
at SortObjects.main(SortObjec
Exception in thread "main"
any help would be appreciated.
Thanks in advance
Business Accounts
Answer for Membership
by: zzynxPosted on 2004-12-10 at 07:40:26ID: 12793180
Try replacing
0]) ); 0]) );
[1]);
Collections.sort(list);
by
Collections.sort(list, new Comparator() {
public int compare(Object o1, Object o2) {
String s1 = (String)o1;
String s2 = (String)o2;
String integer1[] = s1.split("[0-9]");
String integer2[] = s2.split("[0-9]");
String chars1[] = s1.split("[^0-9]");
String chars2[] = s2.split("[^0-9]");
Integer i1 = new Integer( Integer.parseInt(integer1[
Integer i2 = new Integer( Integer.parseInt(integer2[
if (i1.equals(i2)
return chars1[1].compareTo(chars2
else
return i1.compareTo(i2);
}
});