Java:对列表进行排序

Java:对列表进行排序,java,list,sorting,collections,point,Java,List,Sorting,Collections,Point,我有一个点列表,即:list。 e、 g: 以下是我迄今为止编写的代码: Collections.sort(tab,new Comparator<List<Point>>(){ @Override public int compare(List<Point> o1, List<Point> o2) { if(o1 != null && o2 !=null)

我有一个点列表,即:
list
。 e、 g:

以下是我迄今为止编写的代码:

Collections.sort(tab,new Comparator<List<Point>>(){


        @Override
        public int compare(List<Point> o1, List<Point> o2) {
            if(o1 != null && o2 !=null)
            {
                Point var1 = o1.get(0);
                            int var1_x = var1.x;
                            int var1_y = var1.y;
                Point var2 = o2.get(0);
                            int var2_x = var2.x;
                            int var2_y = var2.y;

                            Integer.compare(var1_x, var2_x);
                            Integer.compare(var1_y, var2_y);
                return /* What shall I return ? */;                 
            }
            return 0;
        }
    });
Collections.sort(选项卡,new Comparator(){
@凌驾
公共整数比较(列表o1,列表o2){
如果(o1!=null&&o2!=null)
{
点var1=o1.get(0);
int var1_x=var1.x;
int var1_y=var1.y;
点var2=o2.get(0);
int var2_x=var2.x;
int var2_y=var2.y;
整数。比较(var1\ux,var2\ux);
整数。比较(var1_y,var2_y);
返回/*我应该返回什么?*/;
}
返回0;
}
});
这是我的控制台输出的屏幕截图。我想把这一点整理一下。

“我想根据x和y对外部列表进行排序,因为x是 Llp和y中lp的顺序是每个元素的顺序 有限责任合伙中的有限合伙人。”

您应该使用自定义比较器。由于我假设LLP中的每个列表都有相同数量的元素,并且每个列表只针对一个不同的X,因此以下内容应该可以工作:

例子
//创建数据
List Llp=new ArrayList();
对于(int i=0;i<50;i++)
{
List lp=new ArrayList();
对于(int j=0;j){
返回-1;
}否则{
返回0;
}
}
};
比较器比较器_列=新比较器(){
@凌驾
公共整数比较(点o1,点o2){
int i=o2.x;
int j=o1.x;
if(ij){
返回-1;
}否则{
返回0;
}
}
};
//按行排序
Collections.sort(Llp、comparator_行);
//对列进行排序
对于(列表元素:Llp)
Collections.sort(元素、比较器列);
//打印元素
int rowIndex=0,columnIndex=0;
对于(列表行:Llp)
{
对于(点元素:行)
系统输出打印(“+element.x+”、“+element.y+”);
System.out.println();
}
上面的代码获取每行的第一个元素,并提取X坐标。然后该x坐标用于对所有行进行排序

更新 如果您的数据都混在一起了,您可以使用下面的代码,使用树形图将元素放在它们应该在的位置。结果是一个锯齿状数组

//Create the Data

        List<List<Point>> Llp = new ArrayList<List<Point>>();

        for(int i=0; i< 50; i++)
        {
            List<Point> lp = new ArrayList<Point>();

            for(int j=0; j<50; j++)
            lp.add(new Point((int)(Math.random()*1000), (int)(Math.random()*1000)));

            Llp.add(lp);
        }


        //If all data is mixed up we need to filter into new rows based on X using a TreeMap

        TreeMap<Integer, TreeMap<Integer,Point>> data = new TreeMap<Integer,TreeMap<Integer,Point>>();

        for (List<Point> row : Llp) 
        {
            for (Point element : row) 
            {
                TreeMap<Integer,Point> rowForX;
                if(data.containsKey(element.x))
                    rowForX = data.get(element.x);
                else
                    data.put(element.x, rowForX = new TreeMap<Integer,Point>());    
                    //Create specific row in TreeMap

                rowForX.put(element.y,element);
            }           
        }


        //Convert Sorted TreeMap(s) to Lists

        Llp.clear();
        Iterator<Entry<Integer, TreeMap<Integer, Point>>> it = data.entrySet().iterator();

        while(it.hasNext())
            Llp.add(new ArrayList<Point>(it.next().getValue().values()));


        //Print the elements
        int rowIndex = 0, columnIndex=0;

        for (List<Point> row : Llp) 
        {
            for (Point element : row) 
            System.out.print("("+element.x+ "," + element.y +")|");

            System.out.println();
        }
//创建数据
List Llp=new ArrayList();
对于(int i=0;i<50;i++)
{
List lp=new ArrayList();

对于(int j=0;j您是否考虑过使用?是否尝试使用
Collections.sort()
Collections.sort())
仅对一维列表进行排序。我无法使用我的列表进行排序。请详细说明您要排序什么?列表应如何排序?对我来说,这还不清楚。您要如何对外部列表进行排序?非常感谢。我想这就是我要找的:)抱歉,我还有一个问题,有没有办法让这个比较器同时比较x和y?当然,只需在当前显示
返回0的位置添加一个内部
if
语句;
比较器基本上用于比较列表中的元素。因为列表中有列表,所以需要执行一种外部列表,然后列表中每个列表的排序。我已经更新了我的代码。问题是,你的点是否已经分组成Y行,还是所有数据都混在一起了?@meewoK所有数据都混在一起了。我唯一能做的就是每个点都是用它的x和Y坐标定义的。我将在我的问题中添加一个控制台的屏幕截图,以便它变得更清晰了。
Collections.sort(tab,new Comparator<List<Point>>(){


        @Override
        public int compare(List<Point> o1, List<Point> o2) {
            if(o1 != null && o2 !=null)
            {
                Point var1 = o1.get(0);
                            int var1_x = var1.x;
                            int var1_y = var1.y;
                Point var2 = o2.get(0);
                            int var2_x = var2.x;
                            int var2_y = var2.y;

                            Integer.compare(var1_x, var2_x);
                            Integer.compare(var1_y, var2_y);
                return /* What shall I return ? */;                 
            }
            return 0;
        }
    });
//Create the Data

    List<List<Point>> Llp = new ArrayList<List<Point>>();

    for(int i=0; i< 50; i++)
    {
        List<Point> lp = new ArrayList<Point>();

        for(int j=0; j<50; j++)
        lp.add(new Point((int)(Math.random()*1000), i));

        Llp.add(lp);
    }



    // The comparators
    Comparator<List<Point>> comparator_rows = new Comparator<List<Point>>() {

        @Override
        public int compare(List<Point> o1, List<Point> o2) {
            int i = o2.get(0).y;
            int j = o1.get(0).y;
            if (i < j) {
                return 1;
            } else if (i > j) {
                return -1;
            } else {
                return 0;
            }
        }

    };

    Comparator<Point> comparator_columns = new Comparator<Point>() {

        @Override
        public int compare(Point o1, Point o2) {
            int i = o2.x;
            int j = o1.x;
            if (i < j) {
                return 1;
            } else if (i > j) {
                return -1;
            } else {
                return 0;
            }
        }

    };

    // Sort the rows
    Collections.sort(Llp, comparator_rows);

    // Sort the columns
    for (List<Point> element : Llp) 
        Collections.sort(element, comparator_columns);

    //Print the elements
    int rowIndex = 0, columnIndex=0;

    for (List<Point> row : Llp) 
    {
        for (Point element : row) 
        System.out.print("("+element.x+ "," + element.y +")|");

        System.out.println();
    }
//Create the Data

        List<List<Point>> Llp = new ArrayList<List<Point>>();

        for(int i=0; i< 50; i++)
        {
            List<Point> lp = new ArrayList<Point>();

            for(int j=0; j<50; j++)
            lp.add(new Point((int)(Math.random()*1000), (int)(Math.random()*1000)));

            Llp.add(lp);
        }


        //If all data is mixed up we need to filter into new rows based on X using a TreeMap

        TreeMap<Integer, TreeMap<Integer,Point>> data = new TreeMap<Integer,TreeMap<Integer,Point>>();

        for (List<Point> row : Llp) 
        {
            for (Point element : row) 
            {
                TreeMap<Integer,Point> rowForX;
                if(data.containsKey(element.x))
                    rowForX = data.get(element.x);
                else
                    data.put(element.x, rowForX = new TreeMap<Integer,Point>());    
                    //Create specific row in TreeMap

                rowForX.put(element.y,element);
            }           
        }


        //Convert Sorted TreeMap(s) to Lists

        Llp.clear();
        Iterator<Entry<Integer, TreeMap<Integer, Point>>> it = data.entrySet().iterator();

        while(it.hasNext())
            Llp.add(new ArrayList<Point>(it.next().getValue().values()));


        //Print the elements
        int rowIndex = 0, columnIndex=0;

        for (List<Point> row : Llp) 
        {
            for (Point element : row) 
            System.out.print("("+element.x+ "," + element.y +")|");

            System.out.println();
        }