开发者

Optimized way to get a JList sorted in 2 groups

I have a jlist of users. In which some user has presence 1st and while others have 2nd. SO what I want is to display this list as First it shows users with presence=1st in sorted order and then user with presence=2 in sorted order. Here sorting is done based on user's name. Currently I'm able to do all this things but it takes a long to do it as the list has many users around 250. Also the presence of a user can change any time. I have a socket connection to listen to that and that time I also have to above all things on list to show updated user data. How can I do this in a way that takes 开发者_如何学Pythonless time and don't make my application hang?

Here is what I am doing currently:

    List<User> us = new ArrayList<User>();
    int num = model[j].getSize();
    String[] strArr = new String[num];
    for (int i = 0; i < num; i++) {
        strArr[i] = ((User)model[j].get(i)).getName();
        if(!isDuplicateSortedUser(strArr[i], us))
            us.add((User)model[j].get(i));
    }
    sortArray(Collator.getInstance(), strArr);
    User user;
    List<User> temp2 = new ArrayList<User>();
    List<User> temp1 = new ArrayList<User>();
    for (String string : strArr) {
        for (int i = 0; i < num; i++) {
            user = (User) us.get(i);
            if(user.getName().equals(string)){
                if(!isDuplicateSortedUser(user.getUserid(), temp2) && !temp2.contains(user) && !temp1.contains(user)){
                    if(user.getPresence().toLowerCase().equals("1st"))
                        temp2.add(user);
                    else
                        temp1.add(user);
                }
            }
        }
    }
    int l=0;
    for (User user2 : temp1) {
        model[j].setElementAt(user2, l);
        l++;
    }
    for (User user2 : temp2) {
        model[j].setElementAt(user2, l);
        l++;
    }

Here model is the DefaultListModel of JList. The sortArray method is:

private void sortArray(Collator collator, String[] strArray) {
        String tmp;
        if (strArray.length == 1) return;
        for (int i = 0; i < strArray.length; i++) {
            for (int j = i + 1; j < strArray.length; j++) {
                if(collator.compare(strArray[i], strArray[j] ) > 0 ) {
                    tmp = strArray[i];
                    strArray[i] = strArray[j];
                    strArray[j] = tmp;
                }
            }
        } 
    }

How can I optimize above code?


Put the users in an ArrayList and use the built-in Java sort functionality: http://download.oracle.com/javase/6/docs/api/java/util/Collections.html

This will invoke a mergesort, which will be more efficient than what you've written.

What exactly are you trying to do in the first for loop?

As far as I can see, you only need to do the following:

  1. Loop once over all users, throwing all "1st" users in ArrayList A, all "2nd" users in ArrayList B
  2. Sort A, Sort B,
  3. Merge A and B back together.


As noted by @Jeroen, implementing Comparable is the preferred approach. You can also implement Comparator as shown in RecordComparator or use SortedComboBoxModel.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜