Collections

Sort elements in arraylist java

How to sort arraylist in ascending order in java

test

package com.candidjava;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;

public class ArrayListAscending {

	public static void main(String[] args) {

		ArrayList al = new ArrayList();
		al.add("reena");
		al.add("teena");
		al.add("anu");
		al.add("zahir");
		al.add("sonu");
		System.out.println("Unsorted ArrayList:");
		System.out.println(al);
		// Sorting Arraylist in ascending order
		System.out.println("Arraylist in Ascending order:");
		Collections.sort(al);
		Iterator itr = al.iterator();
		while (itr.hasNext()) {
			String name = (String) itr.next();
			System.out.println(name);
		}

	}

}

OutPut

Unsorted ArrayList:
[reena, teena, anu, zahir, sonu]
Arraylist in Ascending order:
anu
reena
sonu
teena
zahir


How to sort arraylist in descending order in java

test

package com.candidjava;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;

public class ArrayListDescending {
	public static void main(String[] args) {
		ArrayList al = new ArrayList();
		al.add("reena");
		al.add("teena");
		al.add("anu");
		al.add("zahir");
		al.add("sonu");
		System.out.println("Unsorted Arraylist:");
		System.out.println(al);
		// Sorting in descending order
		System.out.println("Arraylist in descending order:");
		Collections.sort(al, Collections.reverseOrder());
		System.out.println(al);
		Iterator itr = al.iterator();
		while (itr.hasNext()) {
			String name = (String) itr.next();
			System.out.println(name);
		}
	}

}

Out Put

Unsorted Arraylist:
[reena, teena, anu, zahir, sonu]
Arraylist in descending order:
[zahir, teena, sonu, reena, anu]
zahir
teena
sonu
reena
anu


How to sort arraylist in java without using sort method

package com.candidjava;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;

public class Arraylistsortwithoutsortmethod {
	public static void main(String[] args) {
		ArrayList al = new ArrayList();
		al.add("reena");
		al.add("teena");
		al.add("anu");
		al.add("zahir");
		al.add("sonu");
		System.out.println("Unsorted Arraylist:");
		System.out.println(al);
		int count = al.size();
		String temp;
		Object[] name = al.toArray();
		for (int i = 0; i < count; i++) {
			for (int j = i + 1; j < count; j++) {
				if (((String) name[i]).compareTo((String) name[j]) > 0) {
					temp = (String) name[i];
					name[i] = name[j];
					name[j] = temp;
				}
			}

		}
		System.out.println("Sorted Arraylist:");
		for (int a = 0; a < name.length; a++) {
			System.out.println(name[a]);
		}

	}
}

Out Put

Unsorted Arraylist:
[reena, teena, anu, zahir, sonu]
Arraylist in descending order:
[zahir, teena, sonu, reena, anu]
zahir
teena
sonu
reena
anu

How to Int sort arraylist in java without using sort method

package com.candidjava;

import java.util.ArrayList;

public class Intarraysortingwithoutsort {
	public static void main(String[] args) {
		ArrayList<Integer> al = new ArrayList<Integer>();
		al.add(10);
		al.add(60);
		al.add(20);
		al.add(70);
		al.add(100);
		System.out.println("unsorted arraylist:");
		System.out.println(al);
		for (int i = 0; i < al.size(); i++) {
			for (int j = i + 1; j < al.size(); j++) {

				if (al.get(i) > al.get(j)) {
					int tmp = al.get(i);
					al.set(i, al.get(j));
					al.set(j, tmp);
				}

			}

		}
		System.out.println("Sorted Arraylist:");
		for (int i : al) {

			System.out.println(i);

		}
	}
}

Out Put

unsorted arraylist:
[10, 60, 20, 70, 100]
Sorted Arraylist:
10
20
60
70
100


Java sort arraylist of objects by field using comparable

package com.candidjava;

import java.util.ArrayList;
import java.util.Collections;

class Author implements Comparable<Author> {
	String authorname;
	String bookname;
	int bookprice;

	// constructor for author
	Author(String authorname, String bookname, int bookprice) {
		this.authorname = authorname;
		this.bookname = bookname;
		this.bookprice = bookprice;
	}

	// Printing Author in the main
	public String toString() {
		return this.authorname + "    " + this.bookname + "    " + this.bookprice;
	}

	public String getAuthorname() {
		return authorname;
	}

	public void setAuthorname(String authorname) {
		this.authorname = authorname;
	}

	@Override
	public int compareTo(Author o) {

		return this.getAuthorname().compareTo(o.getAuthorname());
	}
}

public class Arraylistsortcomparable {
	public static void main(String[] args) {
		ArrayList<Author> al = new ArrayList<Author>();
		al.add(new Author("Shakespeare", "The Tragedy of Macbeth", 300));
		al.add(new Author("Shakespeare", "The Winters Tale", 350));
		al.add(new Author("Vairamuthu", "Moondram ulaga por", 500));
		al.add(new Author("Dr.A.P.J", "Agni siragugal", 200));
		al.add(new Author("James Gosling", "JAVA Programming Language", 400));
		System.out.println("Unsorted Arraylist");
		for (int i = 0; i < al.size(); i++) {
			System.out.println(al.get(i));
		}
		// Ascending Order
		System.out.println("--------------------------------------------");
		System.out.println("Sorted Array list");
		System.out.println("--------------------------------------------");

		Collections.sort(al);
		for (int i = 0; i < al.size(); i++) {
			System.out.println(al.get(i));
		}
	}
}

Out Put

Unsorted Arraylist
Shakespeare    The Tragedy of Macbeth    300
Shakespeare    The Winters Tale    350
Vairamuthu    Moondram ulaga por    500
Dr.A.P.J    Agni siragugal    200
James Gosling    JAVA Programming Language    400
--------------------------------------------
Sorted Array list
--------------------------------------------
Dr.A.P.J    Agni siragugal    200
James Gosling    JAVA Programming Language    400
Shakespeare    The Tragedy of Macbeth    300
Shakespeare    The Winters Tale    350
Vairamuthu    Moondram ulaga por    500

Java arraylist sort comparator

package com.candidjava;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

class Studentdata {
	int rno;
	String name;
	int age;

	// Constructor for Studentdata
	Studentdata(int rno, String name, int age) {
		this.rno = rno;
		this.name = name;
		this.age = age;

	}

	// Used to print studentdetails in main method
	public String toString() {
		return this.rno + " " + this.name + " " + this.age;
	}
}

// This class is for Student name sort
class Namesort implements Comparator<Studentdata> {

	@Override
	public int compare(Studentdata o1, Studentdata o2) {
		return o1.name.compareTo(o2.name);
	}
}

// This class id for Student Roll number sort
class Rollnosort implements Comparator<Studentdata> {

	@Override
	public int compare(Studentdata o1, Studentdata o2) {
		return o1.rno - o2.rno;
	}
}

public class Arraylistsortcomparator {

	public static void main(String[] args) {
		ArrayList<Studentdata> al = new ArrayList<Studentdata>();
		al.add(new Studentdata(15, "reena", 12));
		al.add(new Studentdata(30, "teena", 24));
		al.add(new Studentdata(21, "meena", 20));
		al.add(new Studentdata(21, "seetha", 20));
		System.out.println("Unsorted Arraylist");
		for (int i = 0; i < al.size(); i++) {
			System.out.println(al.get(i));
		}
		// Ascending Order
		System.out.println("Sorted by Name in Ascending order:");
		Collections.sort(al, new Namesort());
		for (int i = 0; i < al.size(); i++) {
			System.out.println(al.get(i));
		}
		System.out.println("Sorted by Rollno in Ascending order:");
		Collections.sort(al, new Rollnosort());
		for (int i = 0; i < al.size(); i++) {
			System.out.println(al.get(i));
		}

		// Descending order
		System.out.println("Sorted by Name in Descending order:");
		Collections.sort(al, Collections.reverseOrder(new Namesort()));
		for (int i = 0; i < al.size(); i++) {
			System.out.println(al.get(i));
		}
		System.out.println("Sorted by Rollno in Descending order:");
		Collections.sort(al, Collections.reverseOrder(new Rollnosort()));
		for (int i = 0; i < al.size(); i++) {
			System.out.println(al.get(i));
		}

	}
}

Out Put

Unsorted Arraylist
15 reena 12
30 teena 24
21 meena 20
21 seetha 20
Sorted by Name in Ascending order:
21 meena 20
15 reena 12
21 seetha 20
30 teena 24
Sorted by Rollno in Ascending order:
15 reena 12
21 meena 20
21 seetha 20
30 teena 24
Sorted by Name in Descending order:
30 teena 24
21 seetha 20
15 reena 12
21 meena 20
Sorted by Rollno in Descending order:
30 teena 24
21 seetha 20
21 meena 20
15 reena 12