• 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
here I have taken some Strings(FirstName<space>SecondName)
I want to sort it by Second list plz help me I am giving the code here. This program sorts on FirstName
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"));
}
}
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since you posted this same question in Java in General (Advanced), I'm closing the topic here. Please don't post the same question in mutilple forums - it is a waste of time for people to pend time replying to this when it's already been answered elsewhere. Thanks.
 
Catch Ernie! Catch the egg! And catch this tiny ad too:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic