java - sorting rows in a file according to a number included in the rows -


i have following text file (offline.txt):

# timestamp, x, y, mac address of ap, rss 1395444273179 35.19967269897461 19.1965389251709 28:c6:8e:85:80:d3 -71 1395444273179 35.19967269897461 19.1965389251709 00:1a:1e:8e:e9:a1 -75 1395444273179 35.19967269897461 19.1965389251709 00:1a:1e:8e:e9:a2 -74 1395444273179 35.19967269897461 19.1965389251709 00:1a:1e:8e:e9:b1 -84 1395444273179 35.19967269897461 19.1965389251709 00:1a:1e:8e:e9:b2 -85 1395444273179 35.19967269897461 19.1965389251709 00:1a:1e:8e:e9:b0 -85 1395444273179 35.19967269897461 19.1965389251709 00:1a:1e:8e:e9:a0 -74 1395444273179 35.19967269897461 19.1965389251709 00:1a:1e:87:04:41 -75 1395444273179 35.19967269897461 19.1965389251709 00:1a:1e:87:04:40 -73 1395444273179 35.19967269897461 19.1965389251709 00:1a:1e:87:04:42 -74 1395444273179 35.19967269897461 19.1965389251709 00:1a:1e:87:04:52 -96 1395444273179 35.19967269897461 19.1965389251709 00:1a:1e:87:04:50 -97 

i sort lines of file according number in 5th column of file in descending order if value repeated doesn't matter order of repeated values.

for example desired output (offline_out.txt) want previous particular text file:

# timestamp, x, y, mac address of ap, rss 1395444273179 35.19967269897461 19.1965389251709 28:c6:8e:85:80:d3 -71 1395444273179 35.19967269897461 19.1965389251709 00:1a:1e:87:04:40 -73 1395444273179 35.19967269897461 19.1965389251709 00:1a:1e:87:04:42 -74 1395444273179 35.19967269897461 19.1965389251709 00:1a:1e:8e:e9:a0 -74 1395444273179 35.19967269897461 19.1965389251709 00:1a:1e:8e:e9:a2 -74 1395444273179 35.19967269897461 19.1965389251709 00:1a:1e:8e:e9:a1 -75 1395444273179 35.19967269897461 19.1965389251709 00:1a:1e:8e:e9:b1 -84 1395444273179 35.19967269897461 19.1965389251709 00:1a:1e:8e:e9:b2 -85 1395444273179 35.19967269897461 19.1965389251709 00:1a:1e:8e:e9:b0 -85 1395444273179 35.19967269897461 19.1965389251709 00:1a:1e:87:04:52 -96 1395444273179 35.19967269897461 19.1965389251709 00:1a:1e:87:04:50 -97 

i know how read file, , know "sort" function in java can me sorting. idea extract numbers in 5th row, save them in vector sort vector , find way assiciate numbers specific rows once number sorted rows sorted, , save them file. ideas on how program this?

this program have far:

public class extract {     public static void main (string[] args) throws java.lang.exception     {     file inputfile = new file("offline.txt");     file tempfile = new file("offline_out.txt");      bufferedreader reader = new bufferedreader(new filereader(inputfile));     bufferedwriter writer = new bufferedwriter(new filewriter(tempfile));      //while read lines, how can store numbers vector associate them specific row?         while((currentline = reader.readline()) != null) {            }     }      //to save output file     boolean successful = tempfile.renameto(inputfile);      } 

use treemap store lines corresponding last number in particular line

treemap<integer, string> map = new treemap<integer, string>(); while((currentline = reader.readline()) != null) {      // split line , use last value key     if (!currentline.contains("timestamp"))         map.put(integer.parseint(currentline.split("\\s+")[4]), currentline);     else         map.put(0, currentline); } 

finally can print , see results(or can write file please):

for(integer key : map.descendingkeyset())     system.out.println(map.get(key)); 

the repeated lines not being captured in above, updating structure capture it

use map arraylist, store lines corresponding particular number

treemap<integer, arraylist<string>> map = new treemap<integer, arraylist<string>>(); while((currentline = reader.readline()) != null) {      int key;     if (!currentline.contains("timestamp"))         // split line , use last value key         key = integer.parseint(currentline.split("\\s+")[4];     else         key = 0;     arraylist<string> lines;     if (!map.contains(key)) //if key doesn't exist create new arraylist         lines = new arraylist<string>();                 else  // if key exists use arraylist in map         lines = map.get(key);     lines.add(currentline);     map.put(key, lines); } 

and printing:

for(integer key : map.descendingkeyset())     for(string line : map.get(key))         system.out.println(line); 

Comments

Popular posts from this blog

user interface - How to replace the Python logo in a Tkinter-based Python GUI app? -

objective c - Greedy NSProgressIndicator Allocation -

how to set an OCR language in Google Drive -