根据用户的动态字段对链接列表进行排序

时间:2021-07-21 07:18:42

My objective is to Dynamically insert values in to the Linked List. And thereafter, I want to perform sorting or search algorithms on the List.
In addition to it, I am creating class at runtime (based on user input) using Reflection.
Thereafter, I use data provided by the user in JSON Array, to create instances of the class, and then I insert the instances in to the GenericList.
Following is the code for the Generic Linked List.

我的目标是动态地将值插入到链接列表中。此后,我想在List上执行排序或搜索算法。除此之外,我使用Reflection在运行时创建类(基于用户输入)。此后,我使用用户在JSON数组中提供的数据来创建类的实例,然后将实例插入到GenericList中。以下是通用链接列表的代码。

public class LinkedListNode<T> implements Serializable {
    private T value;
    private LinkedListNode<T> next;

    public LinkedListNode(T value) {
        this.value = value;
    }
    public void setNext(LinkedListNode<T> next) {
        this.next = next;
    }
    public LinkedListNode<T> getNext() {
        return next;
    }
    public T getValue() {
        return value;
    }
}

public class GenericList<T>  implements Serializable {
    private LinkedListNode<T> first = null;
    public void insert(LinkedListNode<T> node) {
        node.setNext(first);
        first = node;
    }
    public void emptyList(){
        first = null;
    }
    public void remove(){
        if(first.getNext()!=null)
            first = first.getNext();
        else first = null;
    }   
}

And this is how I create instances of the class and insert it to the GenericList.

这就是我创建类的实例并将其插入GenericList的方法。

//dataToInsert => is the JSONArray. =>  [{field1:"value1",field2:"value1"},{field1:"value2",field2:"value2"},{field1:"value3",field2:"value3"}]
//classLoaded =>  package com.LinkedAnalyzerAdapter.saveTestClasses; public class order implements java.io.Serializable  {public String field1;public String field2;}
Class<?> classLoaded = classLoader.loadClass("com.LinkedAnalyzerAdapter.saveTestClasses.order");
GenericList<Object> list = new GenericList<Object>(); 
for (int i = 0; i < dataToInsert.length(); i++) {
    JSONObject jsonObj = new JSONObject();
    jsonObj = dataToInsert.getJSONObject(i);
    Object obj = classLoaded.newInstance();
    Field[] fs = classLoaded.getDeclaredFields();
    for (Field field : fs)
    {
        field.setAccessible(true);
        Object fieldValue = jsonObj.get(field.getName());
        field.set(obj, fieldValue);          
    }   
    list.insert(new LinkedListNode<Object>(obj));           
} 

I am successfully able to insert data in to GenericList, but after inserting I later want to sort the data based on field1, in the ascending order.
I have spent hours to solve it but unable to successfully accomplish sorting.

我成功地能够将数据插入到GenericList中,但是在插入之后我想要按升序对基于field1的数据进行排序。我花了好几个小时来解决它但无法成功完成排序。

2 个解决方案

#1


1  

You should really use java.util.LinkedList instead of your own GenericList, to take advantage of the built in Collections

您应该使用java.util.LinkedList而不是您自己的GenericList,以利用内置的集合

LinkedList<LinkedListNode<?>> list = new LinkedList<>();
Collections.sort(list, new Comparator<String>() {
     @Override
     public int compare(String o1, String o2) {
         return ...
     }
 }

#2


0  

Used the following code to resolve the issue.

使用以下代码来解决此问题。

public void sortLinkedList(final String fieldToCompare){

        Collections.sort(testList, new Comparator<LinkedListNode>() {
            @Override
            public int compare(LinkedListNode arg0, LinkedListNode arg1) {
                // TODO Auto-generated method stub
                Field[] fs = classtoLoad.getDeclaredFields();
                for (Field field : fs){
                    field.setAccessible(true);
                    Object fieldName = field.getName();
                     if(fieldToCompare.equalsIgnoreCase((String) fieldName)){
                         try {
                             String value1 = (String) field.get(arg0.getValue());
                             String value2 = (String) field.get(arg1.getValue());
                             return value1.compareToIgnoreCase(value2);      

                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }       
                         return 0;
                     }
                }
                return 0;
            }
        }); 
}

#1


1  

You should really use java.util.LinkedList instead of your own GenericList, to take advantage of the built in Collections

您应该使用java.util.LinkedList而不是您自己的GenericList,以利用内置的集合

LinkedList<LinkedListNode<?>> list = new LinkedList<>();
Collections.sort(list, new Comparator<String>() {
     @Override
     public int compare(String o1, String o2) {
         return ...
     }
 }

#2


0  

Used the following code to resolve the issue.

使用以下代码来解决此问题。

public void sortLinkedList(final String fieldToCompare){

        Collections.sort(testList, new Comparator<LinkedListNode>() {
            @Override
            public int compare(LinkedListNode arg0, LinkedListNode arg1) {
                // TODO Auto-generated method stub
                Field[] fs = classtoLoad.getDeclaredFields();
                for (Field field : fs){
                    field.setAccessible(true);
                    Object fieldName = field.getName();
                     if(fieldToCompare.equalsIgnoreCase((String) fieldName)){
                         try {
                             String value1 = (String) field.get(arg0.getValue());
                             String value2 = (String) field.get(arg1.getValue());
                             return value1.compareToIgnoreCase(value2);      

                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }       
                         return 0;
                     }
                }
                return 0;
            }
        }); 
}