This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
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


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to sort in descending order" Watch "How to sort in descending order" New topic
Author

How to sort in descending order

Akshayan Venkatesh
Greenhorn

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.

Thanks
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)

J


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: 14114
    
  16

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 7 API documentation
Scala Notes - My blog about Scala
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.

J
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
Hi


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));
s=balance.keySet();
names=(List)s;
//Enumeration en=names.elements();
// Here use use

Iterator i=names.iterator();

Collections.sort(names,Collections.reverseOrder());

while(i.hasNext())
//while(en.hasMoreElements())
{

str=(String) i.next();
//str=(String) names.nextElement();
System.out.println(str+":"+balance.get(str));
}
}
}








//Second One
2//

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));
System.out.println(set);

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

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

}
}

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to sort in descending order