按字母顺序排序java中的输出文件

时间:2014-06-09 14:32:50

标签: java arrays string sorting

我有一个程序从文件中获取数据:

Id     First Name   Last Name
1      John         Smith
2      Joe          Blow
3      Cat          Dog

然后根据上面给出的信息获取该数据并生成用户名和密码:

Id     Username    Password
1      SmJo1       w6o3ju2y
2      BlJo2       kv4nbu4a
3      DoCa3       7gpxlaup

基本上我需要做的是让我的程序按照字母顺序按字母顺序组织输入文件,而后者又按照相同的顺序组织我的输出文件,我不知道如何解决这个问题。 / p>

4 个答案:

答案 0 :(得分:1)

假设您有一个Person类,如下所示:

public class Person {
   private int id;
   private String firstName;
   private String lastName;

   public int getId() {
       return id;
   }
   public void setId(int id) {
       this.id = id;
   }
   public String getFirstName() {
       return firstName;
   }
   public void setFirstName(String firstName) {
       this.firstName = firstName;
   }
   public String getLastName() {
       return lastName;
   }
   public void setLastName(String lastName) {
       this.lastName = lastName;
   }

   public Person(int id, String firstName, String lastName) {
       super();
       this.id = id;
       this.firstName = firstName;
       this.lastName = lastName;
   }
}

您可以使用集合的静态sort方法对Person对象列表进行排序,如下所示:

    List<Person> persons = new ArrayList<Person>();
    persons.add(new Person(1, "John", "Smith"));
    persons.add(new Person(2, "Joe", "Blow"));
    persons.add(new Person(3, "Cat", "Dog"));

    Collections.sort(persons, new Comparator<Person>() {
        public int compare(Person p1, Person p2) {
            String lastName1 = p1.getLastName();
            String lastName2 = p2.getLastName();
            return lastName1.compareTo(lastName2);
        }
    });

答案 1 :(得分:0)

在这种情况下,您可以使用TreeMap,其中您的键可以是姓氏,值可以是逗号分隔的Firstname和ID字符串,或者您的值可以是字符串的ArrayList,其中您将ID和Firstname存储为字符串,稍后检索转换它们。

treeMap将按排序顺序自动存储姓氏。

答案 2 :(得分:0)

创建一个Person类

public class Person implements Comparable<Person> {

   private String firstName;
   private String lastName;
   private String username;
   private String password;

  //do stuff

  public int compareTo(Person p) {
     lastName.compareTo(p.lastName);
  }

  public void writeToFile(FileWriter file) {
   //Implements
 }      

}

之后,缩短元素数组

Collections.sort(persons);

按顺序写人

for (Person p : persons) {
   p.writeToFile(fileWriter);
}

答案 3 :(得分:0)

您将获得的解决方案可能看起来非常相似。这是我的:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class User implements Comparable<User> {
    private int id;
    private String firstName;
    private String lastName;

    public User(int id, String firstName, String lastName) {
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public int compareTo(User other) {
        int lastNameComparison = lastName.compareTo(other.lastName);

        return (lastNameComparison != 0 ? lastNameComparison : firstName
                .compareTo(other.firstName));
    }

    @Override
    public String toString() {
        return String.format("%d\t%s\t%s", id, firstName, lastName);
    }

    public static void main(String... args) {
        List<User> users = new ArrayList<User>();
        users.add(new User(1, "John", "Smith"));
        users.add(new User(2, "Joe", "Blow"));
        users.add(new User(3, "Cat", "Dog"));

        Collections.sort(users);

        for(User user : users) {
            System.out.println(user.toString());
        }
    }
}