How to implement Comparator in Map

Hi,I am using TreeMap to store id,name pair as key & value. Now it is sorted by key. but I want it to sort by value stored in the TreeMap. any one can explain how it can be done, whether comparator interface can be used for these.Thanks in advanceSabarish R L
[291 byte] By [sabarishrla] at [2007-11-26 17:03:21]
# 1

http://java.sun.com/j2se/1.4.2/docs/api/java/util/Comparator.html

// File: arrays/filelist/Filelistsort.java

// Purpose: List contents of user home directory.

// Demonstrates use of Comparators to sort the

// same array by two different criteria.

// Author: Fred Swartz 2006-Aug-23 Public domain.

import java.util.Arrays;

import java.util.Comparator;

import java.io.*;

public class Filelistsort {

//======================================================= main

public static void main(String[] args) {

//... Create comparators for sorting.

Comparator<File> byDirThenAlpha = new DirAlphaComparator();

Comparator<File> byNameLength= new NameLengthComparator();

//... Create a File object for user directory.

File dir = new File(System.getProperty("user.home"));

File[] children = dir.listFiles();

System.out.println("Files by directory, then alphabetical");

Arrays.sort(children, byDirThenAlpha);

printFileNames(children);

System.out.println("Files by length of name (long first)");

Arrays.sort(children, byNameLength);

printFileNames(children);

}

//============================================= printFileNames

private static void printFileNames(File[] fa){

for (File oneEntry : fa) {

System.out.println("" + oneEntry.getName());

}

}

}

////////////////////////////////////////////////// DirAlphaComparator

// To sort directories before files, then alphabetically.

class DirAlphaComparator implements Comparator<File> {

// Comparator interface requires defining compare method.

public int compare(File filea, File fileb) {

//... Sort directories before files,

//otherwise alphabetical ignoring case.

if (filea.isDirectory() && !fileb.isDirectory()) {

return -1;

} else if (!filea.isDirectory() && fileb.isDirectory()) {

return 1;

} else {

return filea.getName().compareToIgnoreCase(fileb.getName());

}

}

}

////////////////////////////////////////////////// NameLengthComparator

// To sort by length of file/directory name (longest first).

class NameLengthComparator implements Comparator<File> {

// Comparator interface requires defining compare method.

public int compare(File filea, File fileb) {

int comp = fileb.getName().length() - filea.getName().length();

if (comp != 0) {

//... If different lengths, we're done.

return comp;

} else {

//... If equal lengths, sort alphabetically.

return filea.getName().compareToIgnoreCase(fileb.getName());

}

}

}

pareshppatela at 2007-7-8 23:31:07 > top of Java-index,Java Essentials,New To Java...