Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to sort in descending order

 
Akshayan Venkatesh
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15436
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Gavin Tranter
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
looks a lot like the post given here
[ June 19, 2007: Message edited by: Gavin Tranter ]
 
Jeremy Botha
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
}
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic