AlphaNumeric Sorting for the array of String
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Anonymous
Posted On:   Friday, January 30, 2004 12:04 PM

hi frens, How do sort a alpha numeric string array.. I tried with java.util.arrays.sort(). but its not Sorting with numeric field .. i've placed the result below [The following things should bearranged in ascending order] 1.Name_10surName.xml 2. Name_10lastName.xml 3. Name_11surName.xml 4. Name_11lastName.xml 5. Name_1surName.xml 6. Name_1lastName.xml 7. Name_2surName.xml 8. Name_2lastName.xml 9. Name_3surName.xml 10. Name_3lastName.xml 11. Name_4surName.xml 12. Name_4lastName.xml 13. Name_5surName.xml 14. Name_5lastNa   More>>

hi frens,


How do sort a alpha numeric string array..


I tried with java.util.arrays.sort(). but its not Sorting with numeric field .. i've placed the result below


[The following things should bearranged in ascending order]




1.Name_10surName.xml

2. Name_10lastName.xml

3. Name_11surName.xml

4. Name_11lastName.xml

5. Name_1surName.xml

6. Name_1lastName.xml

7. Name_2surName.xml

8. Name_2lastName.xml

9. Name_3surName.xml

10. Name_3lastName.xml

11. Name_4surName.xml

12. Name_4lastName.xml

13. Name_5surName.xml

14. Name_5lastName.xml

15. Name_6surName.xml

16. Name_6lastName.xml

17. Name_7surName.xml

18. Name_7lastName.xml

19. Name_8surName.xml

20. Name_8lastName.xml

21. Name_9surName.xml

22. Name_9lastName.xml




i jus need a sample code



Result should be, Like this



5. Name_1surName.xml

6. Name_1lastName.xml

7. Name_2surName.xml

8. Name_2lastName.xml

9. Name_3surName.xml

10. Name_3lastName.xml

11. Name_4surName.xml

12. Name_4lastName.xml

13. Name_5surName.xml

14. Name_5lastName.xml

15. Name_6surName.xml

16. Name_6lastName.xml

17. Name_7surName.xml

18. Name_7lastName.xml

19. Name_8surName.xml

20. Name_8lastName.xml

21. Name_9surName.xml

22. Name_9lastName.xml

1.Name_10surName.xml

2. Name_10lastName.xml

3. Name_11surName.xml

4. Name_11lastName.xml



   <<Less

Re: AlphaNumeric Sorting for the array of String

Posted By:   Bhagyashree_Jayaram  
Posted On:   Monday, February 2, 2004 09:52 PM

Hi,

In this case you need to use a comparator and here is the code.

import java.util.Comparator;
import java.util.StringTokenizer;

public class MyComparator implements Comparator {

/* (non-Javadoc)
* @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
*/
public int compare(Object arg0, Object arg1) {
// TODO Auto-generated method stub
if ((arg1 instanceof String) && (arg0 instanceof String)){
if (((String)arg0).indexOf("Name_") !=-1 && ((String)arg1).indexOf("Name_") !=-1){
if (getNumber((String)arg0) == getNumber((String)arg1)){
return ((String)arg0).compareTo(((String)arg1));
}else if (getNumber((String)arg0) > getNumber((String)arg1))
return 1;
else
return -1;
}
}
return 0;
}

public int getNumber(String value){
StringTokenizer st = new StringTokenizer(value ,"0123456789");
String firstValue = st.nextToken();
String secondValue = st.nextToken();
int retValue =Integer.parseInt(value.substring(value.indexOf(firstValue) + firstValue.length(),value.indexOf(secondValue)));
return retValue;
}


}

and when you call sort pass as follows

Arrays.sort(colls,new MyComparator());

Hope this helps...
Bhagya
About | Sitemap | Contact