java - Create and sort 2d array from file -
Ok so basically I have to complete 2 things with this code 1. Two columns from the file have an array Sort them in 2. And arrange them in the order from the largest to the smallest, based on the second column.
Before I can start the sorting algorithm, I need to sort the array properly. Any help would be great. Thank you. I have listed hyperlink to the contents of the file given below.
Import java.util. *; Import java.io * *; Import java.text *; Public category sorter {public static id = 0; Public static score = 0; Public static zero major (string algos []) {scanner infix = zero; Int ROWS = 21; Int COLS = 2; {InFile = New Scanner (Try New File ("sorter.txt")); } Hold (FileNotFoundException e) {System.out.println ("File not found!"); System.exit (0); } While (inFile.hasNextLine ()) {string str = inFile.nextLine (); String [] parts = str.split (""); String part 1 = parts [0]; String Part 2 = Part [1]; Id = integer.percent (part 1); Score = integer Perspective (Part 2); Int [] [] array1 = new int [id] [score]; For (int i = 0; i
You must create a map ( Preferably a traymap) where the keys are column 1 (student id) and the value is column 2 (test score). Then you can sort it by value Sorting-by-well need to be careful, because the usual way of doing this will not work if two students have the same test score. One way to do this is as follows:
& lt; Kashmir, V comparative & lt; Is expanded? Super V & gt; & Gt; SortedSet & LT; Map.Entry & LT; Kashmir, V & gt; & Gt; Entries Value (map & lt; K, V & gt; map, final integer sequence) {SortedSet & lt; Map.Entry & lt; K, V> & Gt; SortedEntries = new tree plant & lt; & Gt; (New Comparative & lt; Map. Entry & lt; K, V & gt; & gt; () {Public Int Comparison (Map. Entry & lt; K, V & gt; E1, Map. Entry & lt; K , V & gt; E2) {Return (Order & gt; 0) Compare Torrent Duplicates (e1.getValue (), e2.getValue ()): CompareTatin Duplicates (e2.getValue (), e1.getValue ()) ;}}); SortedEntries.addAll (map.entrySet ()); Returns sorted; }
Where
Private static V comparative & lt; Is expanded? Super V & gt; & Gt; Int ComparatorArton Duplicates (VV1, VV2) {Return (v1.com Per to (V2) == -1)? -1 1; }
Comments
Post a Comment