xiac
xiac

Reputation: 77

Sorting numbers in text file android

I am trying to sort the numbers in text file using android. Can you help me? This code is sorting part part not all of them. I mean it sorts 1.A 2.C 20.D 3.C 4.A 5.A like that. It should be 1.A 2.C 3.C 4.A 5.A 6.B 20.D I need to convert integer but how can I do that?

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();
        }

Upvotes: 1

Views: 249

Answers (2)

uniknow
uniknow

Reputation: 938

As remarked above you are currently sorting strings. So the part

        List<String> lineList = new ArrayList<String>();
        while ((inputLine = bufferedReader.readLine()) != null) {
            lineList.add(inputLine);

        }
        fileReader.close();
        Collections.sort(lineList);

Has to be changed in something like:

        Map<Integer, String> list = new TreeMap<>();
        while ((inputLine = bufferedReader.readLine()) != null) {
            System.out.println("Read string is: " + inputLine);
            String[] entries= inputLine.split("\\s+");
            for(String entry: entries) {
                String values[] entry.split(".");
                if (values.size == 2) {
                   // Convert first part in integer      
                   if (values[0]!=null && !values[0].isEmpty()) {
                    System.out.println("Converting string " + value[0] + " to integer");
                    list.put(Integer.valueOf(value[0]), value[1]);
                }
            }
        }
        fileReader.close();

        Stream<Map.Entry<K,V>> sorted =
                list.entrySet().stream()
                .sorted(list.Entry.comparingByValue());

NOTE: Haven't tried the code above myself so there might be problems with it but it should (hopefully) clarify what needs to be done. I'm also not entirely sure whether strings like 2. are properly converted.

Upvotes: 0

Guillaume Barr&#233;
Guillaume Barr&#233;

Reputation: 4218

You are currently sorting String and you need to sort Interger

  1. Parsing "1.A 20.A 30.A 2.B 20.C 3.A"

As it seems that your input file contains a mix of letter an numbers like "1.A 20.A 30.A 2.B 20.C 3.A" you can create en new class implementing Comparable to define your own sort.

class Item implements Comparable<Item>{
    String stringPart;
    Integer intPart;
    String c;

    public Item(String c){
        this.c = c;
        String[] res = c.split("\\.");
        intPart = Integer.valueOf(res[0]);
        stringPart = res[1];
    }

    @Override
    public int compareTo(Item o) {
        if(intPart.intValue() == o.intPart.intValue()){
            return stringPart.compareTo(o.stringPart);
        }else{
            return intPart.compareTo(o.intPart);
        }
    }

    public String getContent(){
        return c;
    }
} 

Once done you can change the list definiton according to the new created class :

List<Item> lineList = new ArrayList<>();

The you can parse the input line to fill the list

List<Item> lineList = new ArrayList<>();
while ((inputLine = bufferedReader.readLine()) != null) {
    String[] res = inputLine.split(" ");
    for(String s : res){
        lineList.add(new Item(s));
    }
}   

Sort the list content calling :

Collections.sort(lineList);

Write your output file :

for (Item i : lineList) {
    out.println(i.getContent());
}

The input

"1.A 20.A 30.A 2.B 20.C 3.A"

Will produce

1.A 2.B 3.A 20.A 20.C 30.A

  1. Parsing "A.1 A.20 A.30 B.2 C.20 A.30"

In case of having an input content switching character and number like this "A.1 A.20 A.30 B.2 C.20 A.30" you just have to modify the following to sort first on the characters and then on the numbers:

The reading of the content of the Item

intPart = Integer.valueOf(res[1]);
stringPart = res[0];

And the sorting method :

@Override
public int compareTo(Item o) {
    if(stringPart.equals(o.stringPart)){
        return intPart.compareTo(o.intPart);
    }else{
        return stringPart.compareTo(o.stringPart);
    }
}

Upvotes: 3

Related Questions