• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Sorting String List

 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I want to sort the string list in the following order.

game1, game2, game3, game10, game20, game110.


I have tried with the below code, but it did not work. Can any one help me.

TreeSet ts = new TreeSet();

ts.add("game1");
ts.add("game10");
ts.add("game110");
ts.add("game3");
ts.add("game2");
ts.add("game20");



Iterator it = ts.iterator();
while (it.hasNext()) {
Object element = it.next();

System.out.println(element);
}


Thanks,
Venkat
 
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I have tried with the below code, but it did not work. Can any one help me.



By default, string objects are sorted in natural order -- meaning ascii order. If you want a different order, you will have to write a comparator, and pass it in the constructor of the TreeSet.

Henry
 
Venkata Sirish
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I found it.

import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;

public class TreeSetTest {

public static void main(String[] args) {

TreeSet ts = new TreeSet(new Comparator() {

public int compare(Object o1, Object o2) {

Emp e1 = (Emp) o1;
Emp e2 = (Emp) o2;

int int1 = e1.getName().compareTo(e2.getName());

int int2 = new Integer(e1.getAge()).compareTo(new Integer(e2.getAge()));

if (int1 == 0) {
return int2;

} else
return int1;
}

});

ts.add(new Emp("game", 1));
ts.add(new Emp("game", 10));
ts.add(new Emp("game", 110));
ts.add(new Emp("game", 2));
ts.add(new Emp("game", 30));


Iterator it = ts.iterator();
while (it.hasNext()) {
// Get element
Emp element = (Emp) it.next();

System.out.println(element.getName() + "\t" + element.getAge());
}

}

}

/////////////


package collectionss;

public class Emp {

private String name;
private int age;

Emp(String name, int age){
this.name=name;
this.age=age;
}
/**
* @return Returns the age.
*/
public int getAge() {
return age;
}
/**
* @param age The age to set.
*/
public void setAge(int age) {
this.age = age;
}
/**
* @return Returns the name.
*/
public String getName() {
return name;
}
/**
* @param name The name to set.
*/
public void setName(String name) {
this.name = name;
}

}


Thanks,
Venkat
 
There's a way to do it better - find it. -Edison. A better tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic