• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

sorting

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my collection I have (FirstName<space>LastName)
how can I sort the list according to last name
here i have tried the program
but not working (It always sorts the list by first name)
import java.util.*;
class TComp implements Comparator{
public int compare(Object a, Object b){
int i, j, k;
String aStr, bStr;
aStr = (String) a;
bStr = (String) b;
i = aStr.indexOf('n');
j = bStr.indexOf('n');
System.out.println("i = "+i+"and j = "+j);
k=bStr.substring(i).compareTo(aStr.substring(j));
if(k==0){
//System.out.println("i = "+i+"and j = "+j);
return aStr.compareTo(bStr);}
else
return k;
}
}
public class TreeMapDemo2{
public static void main(String args[]){
TreeMap tm = new TreeMap();
tm.put(" Sunil Bmar", new Double(3000));
tm.put(" DineshAmar", new Double(100000));
tm.put(" Ravi Chandwani",new Double(2000));
tm.put(" Amit Kumar",new Double(1000));
tm.put(" Bhushan Vasvani",new Double(5000));
tm.put(" Kishan Ratnani",new Double(4000));
Set set = tm.entrySet();
Iterator itr = set.iterator();
while(itr.hasNext()){
Map.Entry me = (Map.Entry)itr.next();
System.out.print(me.getKey()+ ":" );
System.out.println(me.getValue());
}
System.out.println();
double balance = ((Double)tm.get("Sunil kumar")).doubleValue();
tm.put("Sunil kumar",new Double(balance + 1000));
System.out.println("Sunil kumar's new balance : "+tm.get("Sunil kumar"));
}
}
 
Ranch Hand
Posts: 1953
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I have sample code on
http://members.spree.com/education/developergrp/Code/SortBy.htm
which can sort your records by any field your choose. It might be useful for you.
Roseanne
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try and add a method

Then you can change your compare() method with the lines

I hope this helps
Also please review the Naming Policy. It's really for your benifit. Periodically we conduct contests on forums and give awway a book. Only those who post to the forum AND meet the naming policy are entered. A contest is going on on the Servlet forum right now so...
Looking forward to your posts
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic