[英]How do I sort a text file alphabetically?
例如,我想要以下数据:
Manufacturer : - Violin
Type : - Electric
Colour: - Blue
Price : $23
Manufacturer : - Guitar
Type : - Electric
Colour: - Red
Price : $54
看起来像:
Manufacturer : - Guitar
Type : - Electric
Colour: - Red
Price : $54
Manufacturer : - Violin
Type : - Electric
Colour: - Blue
Price : $23
(请注意,小提琴信息在吉他下方是如何排列的,因为按字母顺序位于“ G”吉他之后。)
解决此问题的完整代码将需要较长的时间才能得到答复,但我可以为您提供一些指导:
首先创建一个类,其中包含有关每个对象“ Violin”,“ Guitar”等的信息。基本上,该类如下所示:
public class BlockOfText
{
public String name;
public String block;
}
,其中“名称”是“吉他”和“小提琴”,“块”是整个块。 现在,从输入文件中读取每个块,并将它们保存在数组中。 使块对象可排序(我认为在Java中类似Comparable之类的东西)并对块数组进行排序。 现在,将数组的每个条目写入文件。 :)
您必须读取文件并将每个元素存储在数据结构中。
然后,您只需定义compare方法即可。
大致情况:
class ReadFile {
...
void read(){
List<Product> list = ...
while( readAllLines() ) {
if( line.startWith("*")){
list.add( new Product( line ));
}
}
Collections.sort( list );
}
...
}
然后在Product
定义比较方法
class Product implements Comparable<Product> {
public Product( String fromLine ) {
// take values ...
}
// attributes
...
// etc.
And then
public int compareTo( Product other ) {
return this.name.compareTo( other.name );
}
}
您需要创建一个包含属性的类:
然后,您从文件中读取数据并使用数据创建一个对象,然后将每个对象添加到ArrayList中,您可以在其中使用Collections.sort(...)方法。
然后,您的类可以实现Comparable接口,也可以使用BeanComparator 。 Bean Comparator链接有一个简单的示例,该示例实现Comparable,创建自定义Comparator或使用BeanComparator,因此您无需编写任何自定义代码。
我的建议是逐行读取文件并创建“ Item”类型的普通POJO(其中Item只是您自己编写的类,具有4个实例变量(所有Strings),即制造商,类型,颜色和价格)。
在阅读文件时,您可以为文本文件中的每4行创建一个Item实例(即您的第一个Item的制造商=“ Violin”,类型=“ Electric”,颜色=“ Blue”,价格=“ $ 23”) )。
然后,您可以在创建它们时将它们放置在公共的ArrayList上,一旦完成读取文件,就可以使用以下命令对其进行正确排序:
Collections.sort(itemList, new Comparator<Item>() {
@Override
public int compare(Item o1, Item o2) {
return o1.getManufacturer().compareTo(o2);
}
});
其中itemList是ArrayList<Item>
。
现在,您的列表将被排序,从而遍历整个列表,只需将每个Item中的数据写入到ouptut文件中,就这么简单。
编辑根据要求,我将提供更多详细信息,但是,尽管起初我说过我将按照您的要求使用手动排序,但我不再这样做了。 相反,我将尝试向您解释Collections.sort方法,因为了解如何使用它非常重要。
因此,让我们从Item类开始。 我只是将此类中的所有实例变量都公开,尽管您应该宁愿使用常规的getter和setter方法将它们私有化(请注意,我也省略了package和import语句):
public class Item {
public String manufacturer;
public String type;
public String colour;
public String price;
public Item(String manufacturer, String type, String colour, String price) {
this.manufacturer = manufacturer;
this.type = type;
this.colour = colour;
this.price = price;
}
}
该类将用于存储我们希望排序的数据。 现在让我们看看您的主要方法:
public static void main(String[] args) {
try {
BufferedReader reader = new BufferedReader(new FileReader("inputFile.txt"));
String nextLine = reader.readLine();
List<Item> listOfItems = new ArrayList<Item>();
while (nextLine != null) {
if (!nextLine.equals("\n") && !nextLine.isEmpty()) {
String manufacturer = nextLine;
String type = reader.readLine();
String colour = reader.readLine();
String price = reader.readLine();
Item newItem = new Item(manufacturer, type, colour, price);
listOfItems.add(newItem);
}
nextLine = reader.readLine();
}
reader.close();
Collections.sort(listOfItems, new Comparator<Item>() {
@Override
public int compare(Item o1, Item o2) {
return o1.manufacturer.compareTo(o2.manufacturer);
}
});
PrintWriter writer = new PrintWriter("outputFile.txt");
for (Item item : listOfItems) {
writer.println(item.manufacturer);
writer.println(item.type);
writer.println(item.colour);
writer.println(item.price);
writer.println();
}
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
好了,重要的是要注意Collections.sort的工作方式。 您向该方法传递一个集合(或更具体地说,在我们的示例中为List / ArrayList),它将为您排序该列表中的条目。 最大的问题是如何知道哪个Item对象应该排在第一,第二等等。由于我们要对制造商进行排序,因此需要告诉sort()使用其制造商实例变量来比较Item对象。
这是sort()的第二个参数定义的。 这个new Comparator<Item>
部分告诉sort它将对Item对象进行排序。 sort()将使用我们定义的compare()方法进行排序。 它在排序过程中将项目相互比较,并且compare()方法告诉sort()只是将要分类的项目的制造商进行相互比较,并仅基于此进行排序。
如果您仍然不清楚,请特别向我询问。 我认为,与向您展示如何编写冒泡排序相比,了解此解决方案对您更有益。 如果您只想使用Google“ java气泡排序”,则会弹出大量文章/帖子。
这一切都取决于您如何存储数据。 如果仅使用原始文本文件,则必须编写Java代码来解释数据,对其进行排序并吐出结果。 另外,您可以尝试将数据加载到MySQL等数据库中,然后通过Java与之交互。
假设您可以通过某种方式将数据加载到Java中,则可以执行以下操作:
public Instrument { public String manufacturer; public String type; public String color; public BigDecimal price; public Instrument(String manufacturer, String type, String color, BigDecimal price) { this.manufacturer = manufacturer; this.type = type; this.color = color; this.price = price; } public int compareTo(Object obj) Instrument temp = (Instrument)obj; return this.manufacturer.compareTo(temp.manufacturer); } } Instrument[] instruments = new Instrument[100]; // If you have 100 instruments in your file instruments = LoadInstruments("INSRUMENTS.txt"); // Just an example of loading instruments Arrays.sort(instruments);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.