简体   繁体   English

电话簿按字段的​​字母顺序排序

[英]Phonebook Alphabetical Sorting according to the field First Name

I am trying to get my JavaFx application of a phone book in order to sort alphabetically the contacts list according to the first name. 我正在尝试获取电话簿的JavaFx应用程序,以便根据名字按字母顺序对联系人列表进行排序。 However, the existing loop is not listing the names in the correct order. 但是,现有的循环没有按正确的顺序列出名称。 Here is the sorting method: 这是排序方法:

 public static void sortContactList() {
        try {
        for (int i = 0; i < contactList.length - 1; i++) {
            for (int j = i + 1; i < contactList.length; j++) {
                if ((contactList[j].first.toCharArray()[0]) < (contactList[i].first.toCharArray()[0])) {
                    Entry tmp = contactList[j];
                    contactList[j] = contactList[i];
                    contactList[i] = tmp;
                }
            }
        }
    } catch (NullPointerException exc) {}
}

Below it is shown how the contact list is made up. 下面显示了联系人列表的组成方式。 It is stored in a file with first being the person's first names as strings: 它存储在一个文件中,该文件的名字首先是该人的名字作为字符串:

class Entry {
public String first, last, number, note;
}
public class Phonebookfor1510 {
public static Entry[] contactList;
public static int num_entries;
public static void main(String args[]) throws Exception {
    int i;
    char C;
    String code, Command;
    contactList = new Entry[200];
    num_entries = 0;

The rest of my code works with it. 我的其余代码都可以使用。 I am just wondering why it is not sorting the list of values alphabetically. 我只是想知道为什么它没有按字母顺序对值列表进行排序。 Any help would be greatly appreciated!! 任何帮助将不胜感激!!

The Best thing You can Do is Use TreeSet. 您可以做的最好的事情是使用TreeSet。 Like the given example 像给定的例子

TreeSet<String> stringSet = new TreeSet<>();
        stringSet.add("Mummy");
        stringSet.add("Mahima");
        stringSet.add("Gaurav");
        stringSet.add("Naresh");
        stringSet.add("Bhawna");

        for(String s : stringSet) {
            System.out.println(s);
        }

Output : Bhawna Gaurav Mahima Mummy Naresh 输出: Bhawna Gaurav Mahima妈咪Naresh

I think the second for loop will never end without a NullPointerException . 我认为没有NullPointerException ,第二个for循环将永远不会结束。 You are increasing j , but checking for i<contactList . 您正在增加j ,但要检查i<contactList

You could use Arrays.sort() in this case. 在这种情况下,您可以使用Arrays.sort()

take a look at this sample code: 看一下这个示例代码:

public class Entry implements Comparable<Entry>{
    private Long id;
    private String fullName;
    private String phoneNumber;

    public int compareTo(Entry other) {
        if(other==null) throw new NullPointerException();
        /*
        -1 : phonenumber of current entry is smaller that other
        0  : phonenumber of current entry is equal to other
        +1 : phonenumber of current entry is greater than other
         */
        return (this.getFullName().compareTo(other.getPhoneNumber()));
    }
    public static void main(String[] args){
        Entry[] entriesArray = getEntriesArrayListFromSomewhere();
        Arrays.sort(entriesArray);
    }

   //getters ans setters
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM