📜  使用比较器按降序对Java向量进行排序

📅  最后修改于: 2022-05-13 01:55:01.972000             🧑  作者: Mango

使用比较器按降序对Java向量进行排序

Vector类实现了一个可增长对象数组。向量基本上属于遗留类,但现在它与集合完全兼容。它位于Java.util 包,实现了List接口,因此我们可以使用List 接口的所有方法。

有两种类型的排序技术:

  • 首先是内部排序,即使用预定义的排序方法升序排列 Arrays.sort() 用于原始类数组和包装类数组,而 Collections.sort() 用于集合,这两种方法都按升序对元素进行排序。
  • 第二种技术是传递比较器或在类中实现比较器接口作为两个方法中的第二个参数,并根据需要更改排序顺序。 Comparator 仅适用于包装类类型数组和向量、ArrayList 等集合。

输入:

vector [4,3,2,6,7]

输出:

vector [2,3,4,6,7]

使用 Comparator 按降序向量进行排序的方法

  1. 在类中实现比较器接口,改变元素的自然顺序
  2. 直接在Collections.sort()方法的第二个参数中调用新的比较器,改变元素的顺序

示例 1:覆盖实现 Comparator 类的类中的 compare 方法,然后在调用 Collections.sort() 方法时创建并传递该类的对象作为第二个参数。

Java
// Java program to Sort Java Vector in
// descending order using comparator
 
import java.io.*;
import java.util.*;
 
// Implement comparator of the Integer class
class GFG implements Comparator
{
    // Function to print the elements of the vector
    static void print(Vector Numbers)
    {
        for (Integer number : Numbers)
        {
            // Printing the elements
            System.out.print(number + " ");
        }
    }
 
    public static void main(String[] args)
    {
        // Implementing the vector class
        Vector elements = new Vector<>();
 
        // Adding elements in the vector class
        elements.add(4);
        elements.add(3);
        elements.add(2);
        elements.add(6);
        elements.add(7);
 
        // Before sorting the elements
        System.out.print("Before sorting elements ");
 
        print(elements);
 
        System.out.println();
 
        // Sorting the vector elements in descending
        // order
        Collections.sort(elements, new GFG());
 
        System.out.print("After sorting elements ");
 
        // Printing the elements
        print(elements);
    }
 
    // Implementing compare function
    @Override public int compare(Integer o1, Integer o2)
    {
        // Changing the order of the elements
        return o2 - o1;
    }
}


Java
// Java program to Sort Java Vector in
// descending order using comparator
 
import java.io.*;
import java.util.*;
 
class GFG {
 
    // Function to print the elements of the vector
    static void print(Vector Numbers)
    {
        for (Integer number : Numbers) {
            // Printing the elements
            System.out.print(number + " ");
        }
    }
 
    public static void main(String[] args)
    {
        // Implementing the vector class
        Vector elements = new Vector<>();
 
        // Adding elements in the vector class
        elements.add(4);
        elements.add(3);
        elements.add(2);
        elements.add(6);
        elements.add(7);
 
        // Before sorting the elements
        System.out.print("Before sorting elements ");
 
        print(elements);
        System.out.println();
 
        // Sorting the vector elements in descending
        // order
        Collections.sort(
            elements, new Comparator() {
                @Override
                public int compare(Integer o1, Integer o2)
                {
                    // Changing the order of the elements
                    return o2 - o1;
                }
            });
 
        System.out.print("After sorting elements ");
 
        // Printing the elements
        print(elements);
    }
}



输出
Before sorting elements 4 3 2 6 7 
After sorting elements 7 6 4 3 2

示例 2:在调用 Collections.sort() 方法时覆盖比较函数本身。

Java

// Java program to Sort Java Vector in
// descending order using comparator
 
import java.io.*;
import java.util.*;
 
class GFG {
 
    // Function to print the elements of the vector
    static void print(Vector Numbers)
    {
        for (Integer number : Numbers) {
            // Printing the elements
            System.out.print(number + " ");
        }
    }
 
    public static void main(String[] args)
    {
        // Implementing the vector class
        Vector elements = new Vector<>();
 
        // Adding elements in the vector class
        elements.add(4);
        elements.add(3);
        elements.add(2);
        elements.add(6);
        elements.add(7);
 
        // Before sorting the elements
        System.out.print("Before sorting elements ");
 
        print(elements);
        System.out.println();
 
        // Sorting the vector elements in descending
        // order
        Collections.sort(
            elements, new Comparator() {
                @Override
                public int compare(Integer o1, Integer o2)
                {
                    // Changing the order of the elements
                    return o2 - o1;
                }
            });
 
        System.out.print("After sorting elements ");
 
        // Printing the elements
        print(elements);
    }
}


输出
Before sorting elements 4 3 2 6 7 
After sorting elements 7 6 4 3 2