按姓氏排序 [英] Sorting on Last Name

查看:108
本文介绍了按姓氏排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从三个文件中读取数据,然后希望按照某些条件对记录进行排序

I am reading data from three files and then i want to sort the records on some criteria

每个文件都具有以下格式的记录

Each file has records in the following format

名字姓性别颜色日期

这是我拥有的代码...我只能使用collections.sort对名字进行排序...我如何对其他列进行排序...任何帮助将不胜感激..

This is the code which i have ... i can sort only on the first name using collections.sort ... how do i sort on other columns ... any help would be appreciated ..

import java.io.BufferedInputStream;
import java.io.*;
import java.sql.*;
import java.io.RandomAccessFile;
import java.io.*;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.lang.Object;
import java.util.Vector;
import java.util.*;

/**
 * This program reads a text file line by line and print to the console. 
 * It uses FileOutputStream to read the file.
 */

public class test1 {    

  public static void main(String[] args) 
    {
          try {

        ArrayList<String> rows = new ArrayList<String>();
        //  ArrayList<String> rows1 = new ArrayList<String>();
    //          ArrayList<String> rows2 = new ArrayList<String>();
    //              ArrayList<String> rows3 = new ArrayList<String>();
    BufferedReader comma = new BufferedReader(new FileReader("C:\\comma.txt"));
    BufferedReader pipe = new BufferedReader(new FileReader("C:\\pipe.txt"));
    BufferedReader space = new BufferedReader(new FileReader("C:\\space.txt"));

    String c= "";
    String p;
    String s;
    while((c = comma.readLine()) != null) {         
            rows.add(c);        
    }

      Collections.sort(rows);
    //  Collections.sort(rows2);

    FileWriter writer = new FileWriter("output.txt");
    for(String cur: rows)
    {        
        writer.write(cur+"");          
    }
   //  for(String cur: rows1)
    //  writer.write(cur+"\n");
     // for(String cur: rows2)
      // writer.write(cur+"\n");
    comma.close();
  //  pipe.close();
  //  space.close();
    writer.close();

          }
     catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }        
  }
}

推荐答案

您当前正在根据代表整行的字符串按词法排序.在您的文件格式中,该行以名字开头,因此这就是您要用来排序的内容.

You are currently sorting lexically based on the string representing an entire line. In your file format, the line starts with first names, so that's what you are using to sort.

如果要按姓氏排序,则实际上除了解析每一行外别无其他方法,以便程序可以识别姓氏.这会使您的程序更加复杂.

If you want to sort by last name, you really have no way but to parse each line so that the program can identify the last name. That will make your program a lot more complex.

您可以使用StringTokenzier或正则表达式来标识字符串中的姓氏. 然后,您可以使用自定义比较器进行排序.

You can use a StringTokenzier or a regular expression to identify the last name in the string. Then you can use a custom comparator to sort based on that.

另一种OOP方法是将每个记录表示为一个对象,然后根据其字段使用自定义比较器使用Collections.sort().这很长,但这是正确的面向对象的方式".

A more OOP way is to represent each record as an object, and then use Collections.sort() using a custom comparator based on its fields. It's lengthy, but it's the correct "object oriented way".

如果您只想要一种快速而肮脏的解决方案,则可能希望使用更适合文本操作的语言,例如Perl或Python ...

If you just want a quick and dirty solution, you may want to use a language more appropriate for text manipulation, like Perl or Python...

这篇关于按姓氏排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆