Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!news-in-01.newsfeed.easynews.com!easynews!core-easynews-01!easynews.com!en-nntp-15.dc1.easynews.com.POSTED!not-for-mail From: Ben Newsgroups: comp.lang.java.programmer Message-ID: <2012043021273098748-no@waycom> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1; format=flowed Content-Transfer-Encoding: 8bit Subject: Sorting numeric strings User-Agent: Unison/2.1.7 Lines: 63 X-Complaints-To: abuse@easynews.com Organization: Forte Inc. http://www.forteinc.com/apn/ X-Complaints-Info: Please be sure to forward a copy of ALL headers otherwise we will be unable to process your complaint properly. Date: Mon, 30 Apr 2012 21:27:30 -0400 X-Received-Bytes: 2691 Xref: csiph.com comp.lang.java.programmer:14062 Given the following data: Col1, Col2, Col3 438.23, 991897664, ccc 22.12, 991897631, bbb 100.99, 881897631, aaa 50.12, 991884803, ddd The class below will sort the data based on the column specified, except Col1, which contains float values. If you set the SortCol variable below to 0, sorting does not work. If you set it to 1 or 2, sorting does work. How can I sort Col1 which is a column of numeric strings? import java.io.BufferedReader; import java.io.FileReader; import java.io.FileWriter; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.TreeMap; public class SortColumn { public static void main(String[] args) throws Exception { BufferedReader reader = new BufferedReader(new FileReader("file.csv")); //BufferedReader reader = new BufferedReader(new FileReader("jtp-input2-test.csv")); Map> map = new TreeMap>(); String line = reader.readLine(); //read header while ((line = reader.readLine()) != null) { String key = getField(line).toString(); List l = map.get(key); if (l == null) { l = new LinkedList(); map.put(key, l); System.out.println(key); } l.add(line); } reader.close(); FileWriter writer = new FileWriter("sorted_numbers.txt"); writer.write("Col1, Col2, Col3\n"); // writer.write("billnumber, Copay, Discount, NonAllow, unknown\n"); for (List list : map.values()) { for (String val : list) { writer.write(val); writer.write("\n"); } } writer.close(); } private static String getField(String line) { // Column you want to sort on (Zero based) int SortCol = 0; return line.split(",")[SortCol]; } }