i am having a string arraylist 'names'.which contains names of people.i want to sort the arraylist in alphabetical order.plz help me
Asked
Active
Viewed 6.1k times
3 Answers
63
This will solve your problem...
ArrayList arrayList = new ArrayList();
//Add elements to Arraylist
arrayList.add("1");
arrayList.add("3");
arrayList.add("5");
arrayList.add("2");
arrayList.add("4");
Collections.sort(arrayList);
//display elements of ArrayList
System.out.println("ArrayList elements after sorting in ascending order : ");
for(int i=0; i<arrayList.size(); i++)
System.out.println(arrayList.get(i));
To sort an ArrayList object, use Collection.sort
method. This is a
static method. It sorts an ArrayList object's elements into ascending order.
Just in case if the below code in comment doesnt work means...
Try this code..
Create a custom comparator class:
import java.util.Comparator;
class IgnoreCaseComparator implements Comparator<String> {
public int compare(String strA, String strB) {
return strA.compareToIgnoreCase(strB);
}
}
Then on your sort:
IgnoreCaseComparator icc = new IgnoreCaseComparator();
java.util.Collections.sort(arrayList,icc);

Hussain
- 5,552
- 4
- 40
- 50
-
i tried this.it s working but the problem is my arraylist contains both names starts with capital letter and small letter.by using this logic it is displaying sorted list of names starting with capital letter first and then others.i want to sort both capital and small at a time. – andro-girl Apr 28 '11 at 07:46
-
At the same time means, U need to ignore the case and sort according to it, or sort based on case...? – Hussain Apr 28 '11 at 09:34
-
@Seethelakshmi: Try using this `Collections.sort(path, String.CASE_INSENSITIVE_ORDER);`, This wil ignore the case and compare u the String... – Hussain Apr 28 '11 at 09:39
-
@Seethalakshmi: I have editd my answer tak a look at it :) – Hussain Apr 28 '11 at 09:43
-
@ Hussain, thanks for the answers. I've tried your solution for the ArrayList from A1 thru A10, but the A10 didn't sort correctly. Somehow, the A10 goes after A1. Basically, it sorted like A1, A10, A2, A3, etc. Why did it happen and how can I sort list correctly? – Dante Oct 05 '14 at 16:36
-
A1, A10, A2, A3 _is_ correct lexicographic sorting. For general sorting, such as filenames, this is the best technique. On the other hand, if you know your strings will always end with numbers, you can separate them into pieces and compare the numeric and non-numeric parts separately. In either case, I'm going to advise against so-called "natural" sorting that attempts to infer structure from random strings. Every such algorithm produces slightly different and thus unpredictable results, a cardinal sin for any sorting algorithm. – j__m Feb 15 '15 at 02:59
9
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class ArrayListSortExample {
public static void main(String[] args) {
/*
* Create a collections of colours
*/
List colours = new ArrayList();
colours.add("red");
colours.add("green");
colours.add("blue");
colours.add("yellow");
colours.add("cyan");
colours.add("white");
colours.add("black");
/*
* We can sort items of a list using the Collections.sort() method.
* We can also reverse the order of the sorting by passing the
* Collections.reverseOrder() comparator.
*/
Collections.sort(colours);
System.out.println(Arrays.toString(colours.toArray()));
Collections.sort(colours, Collections.reverseOrder());
System.out.println(Arrays.toString(colours.toArray()));
}

evilone
- 22,410
- 7
- 80
- 107
-
2ur ansr is also useful 4 me.but d same prblm stated abv arises.if u can plz help me – andro-girl Apr 28 '11 at 07:59
1
hi seethalakshmi for sorting on Arraylist you need comparator and collection
just go through this link http://ventrix.nsdc.gr/code_folds/?p=119, you can easily implement it