[英]Sort contents of file in alphabetical order into a new file using methods (Java)
So I am trying write some code to find a way to bubble sort an existing text file that I have collected the information in a form of an array and it looks like this:所以我正在尝试编写一些代码来找到一种对现有文本文件进行冒泡排序的方法,我以数组的形式收集了信息,它看起来像这样:
04/26/16 Sega 3D Classics Collection 07/14/16 Batman: Arkham Underworld 06/24/16 Tokyo Mirage Sessions #FE
Essentially I want them to be in alphabetical order and it should make a brand new file that looks like this:本质上,我希望它们按字母顺序排列,它应该创建一个如下所示的全新文件:
Batman: Arkham Underworld Sega 3D Classics Collection Tokyo Mirage Sessions #FE
The problem is I do not know how to remove the date of the string that it is originally part of.问题是我不知道如何删除它最初属于的字符串的日期。 I think there should be some kind of function like indexOf() like in javascript.
我认为应该有某种 function,比如 javascript 中的 indexOf()。 I would assume that after the extraction of the names.
我会假设在提取名称之后。 Then the sorting would be the same as the one I have done for the sorting of dates.
然后排序将与我为日期排序所做的排序相同。
This is the code I have done:这是我所做的代码:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class Main{
public static void main (String[]args) throws IOException{
File file = new File("C:/Users/cyrus/Downloads/Lab 11/releasedates.txt");
String []arr = input(file);
output(file,arr);
outputSort1(file, arr);
}
public static String[]input (File file) throws FileNotFoundException{
String[]arr = new String[3];
Scanner sc = new Scanner(file);
for(int i = 0; i < arr.length; i++){
arr[i] = sc.nextLine();
}
return arr;
}
public static void output(File file, String[] info) throws IOException{
FileWriter writer = new FileWriter("C:/Users/cyrus/Downloads/Lab 11/fileName.txt");
for(String aString:info){
writer.write(aString);
}
writer.close();
}
public static void sortByMonth(String[]info){
String temp;
for (int j = 0; j < info.length; j++) {
for (int i = j + 1; i < info.length; i++) {
if (info[i].compareTo(info[j]) < 0) {
temp = info[j];
info[j] = info[i];
info[i] = temp;
}
}
}
}
public static void outputSort1(File file,String[] info) throws IOException{
sortByMonth(info);
FileWriter writer = new FileWriter("C:/Users/cyrus/Downloads/Lab 11/fileNameSorted1.txt");
for(String aString:info){
writer.write(aString);
}
writer.close();
}
public static void sortByName(String[]info){
//this isn't really finished, I am unsure on how to extract JUST the name of the games
}
}
There are several ways to sort the lines.有几种方法可以对行进行排序。 suppose you put all the lines in a list and sort that list, before the sort, you also remove the date.
假设您将所有行放在一个列表中并对该列表进行排序,在排序之前,您还删除了日期。 Using Java Stream 8
使用 Java Stream 8
List<String> lines = Arrays.asList("04/26/16 Sega 3D Classics Collection",
"07/14/16 Batman: Arkham Underworld",
"06/24/16 Tokyo Mirage Sessions #FE");
List<String> newLines = lines.parallelStream()
.map(line-> line.substring(line.indexOf(" ")))
.sorted((line1, line2)->line1.compareTo(line2))
.collect(Collectors.toList());
newLines.forEach(System.out::println);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.