如何对用户定义的LinkedList进行排序

时间:2014-04-29 20:17:45

标签: java sorting linked-list

我一直试图对链表进行排序,但一切都无济于事。我的格式是:

  1. firstname排序,
  2. age
  3. 姓氏以外的任何其他号码。
  4. 我已在下面发布了我的代码段。但每次我运行程序时,都会收到错误说 "This file cannot be accessed because another process is using the file"
    我将感谢您的贡献。我的所有其他代码运行完美没有问题。我非常肯定。排序是我唯一的问题。非常感谢

    public void sort(int order) {
    
         switch(order){
         case 1:
                Node tempVar;
                for (Node cur = head; cur != null; cur = cur.next) {
    
                    for (Node cux = head; cux != null; cux = cux.next) {
    
                         if(compareByFN(cur, cux)==1)
                            {
                                   tempVar = cux;
                                   cux= cur;
                                   cur = tempVar;
                            }
                    }
    
                }
             break;
         case 2:
                Node tempVar3;
                for (Node cur = head; cur != null; cur = cur.next) {
    
                    for (Node cux = head; cux != null; cux = cux.next) {
    
                         if(compareByAge(cur, cux)==1)
                            {
                                   tempVar3 = cux;
                                   cux= cur;
                                   cur = tempVar3;
                            }
                    }
    
                }
             break;
         default:
                Node tempVar2;
                for (Node cur = head; cur != null; cur = cur.next) {
    
                    for (Node cux = head; cux != null; cux = cux.next) {
    
                         if(compareByLN(cur, cux)==1)
                            {
                                   tempVar2 = cux;
                                   cux= cur;
                                   cur = tempVar2;
                            }
                    }
    
                }
             break;
         }
    
     }
    
     private  int compareNulls(Node event1, Node event2)
        {
            if (event1 == null && event2 == null)
                return 0;
            else if (event1 == null )
            {
                return -1;
            }
            else 
            {
                return 1;
            }
        }
    
     private  int compareByFN(Node event1, Node event2) {
            int comp = event1.data.getFn().compareTo(event2.data.getFn());
            if (event1 == null || event2 == null)
                return compareNulls(event1, event2);
            else if (comp==0) return 0;
            else if (comp<0) return -1;
            else return 1;
        }
    
        private  int compareByLN(Node event1, Node event2) {
            int comp = event1.data.getLn().compareTo(event2.data.getLn());
            if (event1 == null || event2 == null)
                return compareNulls(event1, event2);
            else if (comp==0) return 0;
            else if (comp<0) return -1;
            else return 1;
        }
    
    
        private int compareByAge(Node event1, Node event2) {
            return compare(event1.data.getDob(), event2.data.getDob());
        }
    
        private int compare(Date date1, Date date2) {
            int result;
    
            if (date1 == null) {
    
    
                if (date2 == null)
    
                    result = 0;
                else
    
                    result = -1;            
            }
            else if (date2 == null)
    
                result = 1;
            else {
                if (date1.getYear() == date2.getYear()) {
    
                    if (date1.getMonth() == date2.getMonth()) {
    
    
                        if (date1.getDay() == date2.getDay())
    
    
                            result = 0;
                        else if (date1.getDay() < date2.getDay())
    
                            result = -1;
                        else
    
                            result = 1;
                    }
                    else if (date1.getMonth() < date2.getMonth())
    
                        result = -1;
                    else
    
                        result = 1;
                }
                else if (date1.getYear() < date2.getYear())
    
                    result = -1;
                else
    
                    result = 1;
            }
    
            return result;
        }   
    

0 个答案:

没有答案