void sortPrice () throws IOException {
//create a bufferedReader and user scanner
BufferedReader br = new BufferedReader(new FileReader("items.txt"));
Scanner sc = new Scanner(System.in);
List lines = new ArrayList();
//create a 2D arrayList and append each line of the txt file into a sub-array in the 2D array
for(String line = br.readLine(); line != null; line = br.readLine()){
String[] currentLine = line.split(",");
lines.add(currentLine);
}
String[][] strings = (String[][]) lines.toArray(new String[lines.size()][]);
I have this code that read from items.txt file and turn it into 2d array
I001-2001, Dell XPS 17,5.5, Laptop
I002-1988, Macbook Pro 15, 8.5, Laptop
I003-1992, Samsung Galaxy S22, 4.5, Phone I004-1999, Iphone 13 PM, 7.5, Phone
I005-2015, Harman Kardon, 3.5, Earbud
This is what inside the file TXT. I want to sort the item by the third element inside this sub-array which is the price for the item, but since I created the array as the String, it cannot be sort with interger value, I'm new to java so sorry for any techinicality.
CodePudding user response:
There is a helper function Arrays.sort
(Docs).
It takes two arguments, an array of things (technically objects, in practice, anything including other arrays), and a custom comparator as a second parameter.
Therefore, you can create a custom comparator like this:
public class MyProductComparator implements Comparator<String[]> {
@Override
public int compare(String[] prod1, String[] prod2) {
// Compare the element at index 2 of both arrays
return prod1[2].compareTo(prod2[2]);
}
}
Afterward, you can use it like this:
Arrays.sort(strings, new MyProductComparator());
Additionally, java already provides some standard comparator implementations, for example, one that takes two elements and compares them based on a predefined key. This is quite useful and simplifies your code greatly.
// compare using the second element
Arrays.sort(strings, Comparator.comparing(o -> o[2]))
CodePudding user response:
Files.lines
will read in the file and put each line in a stream for processing. The lines can then be sorted before collecting into a list.
- map the lines to an
Abstract.SimpleEntry
to hold the actual line as the key and the double as the value on which to sort. - The value is split on comma, trimmed of whitespace and retrieved as item 2. Then converted to a Double.
- then sort the stream using the entry's value.
void sortPrice () throws IOException {
list = Files.lines(Path.of("f:/items.txt"))
.map(item -> new SimpleEntry<String, Double>(item,
Double.parseDouble(
item.split(",")[2].trim())))
.sorted(Entry.comparingByValue()).map(Entry::getKey)
.toList();
}
list.forEach(System.out::println);
prints
I005-2015, Harman Kardon, 3.5, Earbud
I003-1992, Samsung Galaxy S22, 4.5
001-2001, Dell XPS 17,5.5, Laptop
Phone I004-1999, Iphone 13 PM, 7.5, Phone
I002-1988, Macbook Pro 15, 8.5, Laptop
Note: if you are going to make use of these fields, it would be best to create a class to hold them in their final converted types.