Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Sorting String List

 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • 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: 23878
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Hoo hoo hoo! Looks like we got a live one! Here, wave this tiny ad at it:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic