-1

I am trying to sort the lines in text file using android. Can you help me? This code is sorting part part not all of them. I mean it sorts 1-2-3-20-4-5-6 like that. It should be 1-2-3-4-5-6-20

         try
            {

               File root = new File(Environment.getExternalStorageDirectory(), "Notes");
                if (!root.exists()) 
                {
                    root.mkdirs();
                }
                File gpxfile = new File(root, fileName);
                FileWriter writer = new FileWriter(gpxfile,true);
                writer.append(s+"\n\n");
                writer.flush();
                writer.close();
                FileReader fileReader = new FileReader(gpxfile);
                BufferedReader bufferedReader = new BufferedReader(fileReader);
                String inputLine;
                List<String> lineList = new ArrayList<String>();
                while ((inputLine = bufferedReader.readLine()) != null) {
                    lineList.add(inputLine);
                }
                fileReader.close();
                Collections.sort(lineList);
                System.out.println(lineList);
                FileWriter fileWriter = new FileWriter(gpxfile);
                PrintWriter out = new PrintWriter(fileWriter);
                for (String outputLine : lineList) {
                    out.println(outputLine);
                }
                out.flush();
                out.close();
                fileWriter.close();
                //Toast.makeText(this, "Data has been written to Report File", Toast.LENGTH_SHORT).show();
            }
            catch(IOException e)
            {
                 e.printStackTrace();
            }
xiac
  • 77
  • 1
  • 8
  • 1
    I suspect the order is 1-2-20-3-4-5-6 not 1-2-3-20-4-5-6. – William_Wilson May 03 '16 at 16:55
  • @William_Wilson It was a sample it can be but how can i sort it – xiac May 03 '16 at 16:57
  • How the code is sorting it now is important. If my suggested order is the actual output then Tim's solution below is correct and they are being sorted as strings instead of numbers. If you are actually getting 3 before 20 then the current sorting is doing something far more strange. – William_Wilson May 03 '16 at 16:59
  • 1
    `"20" < "3"`. If you don't agree with that, read https://en.wikipedia.org/wiki/Lexicographical_order#Ordering_of_sequences_of_various_lengths – njzk2 May 03 '16 at 17:06

2 Answers2

2

You need to sort the strings as actual numbers. Right now they are being sorted as alphanumeric strings.

public class CustomComparator implements Comparator<String> {
    @Override
    public int compare(String s1, String s2) {
        return Integer.valueOf(s1).compareTo(Integer.valueOf(s2));
    }
}

Collections.sort(lineList, new CustomComparator());

If, for some reason, you cannot create a new class, then you can use an inline comparator:

Collections.sort(lineList, new Comparator<String>() {
    @Override
    public int compare(String s1, String s2) {
        return Integer.valueOf(s1).compareTo(Integer.valueOf(s2));
    }
});
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

To complete Tim's answer, I suggest you read this thread: How to use Collections.sort() in JAVA ?

What I would do, is the following: Instead of Collections.sort(lineList); I would use lineList.sort(new CustomComparator)

where

public class CustomComparator implements Comparator<String> {
    @Override
    public int compare(String s1, String s2) {
        return Integer.valueOf(s1).compareTo(Integer.valueOf(s2));
    }
}

as previously suggested.

Community
  • 1
  • 1