jQuery in Action, 3rd edition
The moose likes Java in General and the fly likes How to sort in descending order Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to sort in descending order" Watch "How to sort in descending order" New topic

How to sort in descending order

Akshayan Venkatesh

Joined: Apr 13, 2006
Posts: 26
How to sort in descending order the followings(using util packages)

Name No
xxx 101
yyy 102
aaa 103
ddd 104

please answer the Question above in advance.

Jeremy Botha
Ranch Hand

Joined: Feb 16, 2005
Posts: 125
Sorting by name - sort in reverse alphabetical order.

Sorting by number - sort by numerical comparison.

It's quite simple, really.

(this has the scent of a homework problem)


McFinnigan? Never heard of him. Nobody here but us chickens...<br /> <br />SCJP for Java 1.4<br />SCJD for Java 5.0
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15082

Suppose that you have your data in a List (for example an ArrayList or a LinkedList). You could then use one of the sort(...) methods in class java.util.Collections to sort your list. To provide for different methods of sorting (for example ascending or descending) you could call the sort(...) method that takes a List and a Comparator. The Comparator is responsible for comparing the objects that you are sorting and so determines the sort order.

See the Java API documentation of java.util.Collections.sort(...) and java.util.Comparator.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
looks a lot like the post given here
[ June 19, 2007: Message edited by: Gavin Tranter ]
Jeremy Botha
Ranch Hand

Joined: Feb 16, 2005
Posts: 125
I get the feeling that there are one or two school or University classes on here, given the frequency that similar questions pop up.

Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
Yeah it seems that way.
Nobody did my homework for me. Espically when it came to the code assignment, was a challenge to get the answers myself.

Thats not to say we were saints
Jetendra Ivaturi
Ranch Hand

Joined: Feb 08, 2007
Posts: 159

Try any one of these

// 1.
import java.util.*;
class testing
{ public static void main(String args[])
{ Hashtable balance= new Hashtable();
List names;
Set s;
String str;
double bal;
balance.put("jetendra",new Double(3434.34));
balance.put("tendra",new Double(5000.34));
balance.put("jet",new Double(4034.34));
balance.put("jetdra",new Double(9304.34));
//Enumeration en=names.elements();
// Here use use

Iterator i=names.iterator();



str=(String) i.next();
//str=(String) names.nextElement();

//Second One

import java.util.Arrays;
import java.util.Comparator;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

public class Backwards {
public static void main(String args[]) {
Employee emps[] = { new Employee("Finance", "Degree, Debbie"),
new Employee("Finance", "Grade, Geri"),
new Employee("Finance", "Extent, Ester"),
new Employee("Engineering", "Measure, Mary"),
new Employee("Engineering", "Amount, Anastasia"),
new Employee("Engineering", "Ratio, Ringo"),
new Employee("Sales", "Stint, Sarah"),
new Employee("Sales", "Pitch, Paula"),
new Employee("Support", "Rate, Rhoda"), };
SortedSet set = new TreeSet(Arrays.asList(emps));

try {
Object last = set.last();
boolean first = true;
while (true) {
if (!first) {
System.out.print(", ");
last = set.headSet(last).last();
} catch (NoSuchElementException e) {

Set subset = set.headSet(emps[4]);


class EmpComparator implements Comparator {

public int compare(Object obj1, Object obj2) {
Employee emp1 = (Employee) obj1;
Employee emp2 = (Employee) obj2;

int nameComp = emp1.getName().compareTo(emp2.getName());

return ((nameComp == 0) ? emp1.getDepartment().compareTo(
emp2.getDepartment()) : nameComp);

class Employee implements Comparable {
String department, name;

public Employee(String department, String name) {
this.department = department;
this.name = name;

public String getDepartment() {
return department;

public String getName() {
return name;

public String toString() {
return "[dept=" + department + ",name=" + name + "]";

public int compareTo(Object obj) {
Employee emp = (Employee) obj;
int deptComp = department.compareTo(emp.getDepartment());

return ((deptComp == 0) ? name.compareTo(emp.getName()) : deptComp);

public boolean equals(Object obj) {
if (!(obj instanceof Employee)) {
return false;
Employee emp = (Employee) obj;
return department.equals(emp.getDepartment())
&& name.equals(emp.getName());

public int hashCode() {
return 31 * department.hashCode() + name.hashCode();

SCJP 1.4 & 1.5, SCWCD 1.5. Learn and Let Learn.
Consider Paul's rocket mass heater.
subject: How to sort in descending order
It's not a secret anymore!