java - Java 中的自定义比较器可以工作,但有一个缺陷

标签 java sorting comparator

我需要编写一个算法,接受用户发送的 10 个项目,无论是字符串还是数字,然后将其放入数组中,我的程序应该对数组进行排序。我不被允许使用Java的方法进行比较或排序。应该是我自己的代码。

我编写的程序运行得很好,它可以很好地对字符串进行排序,也可以很好地对单位数字进行排序。

但是,如果输入两位数,则会将其视为一位数,因为我的程序会查看第一个字符进行比较。例如,1 和 10 将相邻排序。我知道问题是什么,但我不知道如何编写自己的接受通用对象的比较器类。

这是我的代码。

import java.util.Scanner;
public class Main
{

    public static void main(String[] args)
    {
        Object items[] = new Object[10];
        Object item;
        Scanner scanner = new Scanner(System.in);
        SelectionSorter sorter = new SelectionSorter();

        System.out.println("Please enter 10 items to be sorted: ");

        for (int i = 0; i < items.length; i++)
        {
            item = scanner.nextLine();

            items[i] = item;
        }
        System.out.println();
        System.out.println("Here are the items in ascending order: ");
        items = sorter.sortInAscendingOrder(items);
        printArray(items);
        System.out.println();

        System.out.println();
        System.out.println("Here are the items in descending order: ");
        items= sorter.sortInDescendingOrder(items );
        printArray(items);
    }

    public static void printArray(Object[] items)
    {
        for (int i = 0; i < items.length - 1; i++)
        {
            System.out.print(items[i] + ",");
        }
        System.out.print(items[items.length - 1]);
    }
}

public class SelectionSorter
{
    Object temp;
    Compare compare;

    public SelectionSorter()
    {

        temp = "";
        compare = new Compare();
    }

    public Object[] sortInAscendingOrder(Object[] n)
    {
        for (int i = 0; i < n.length; i++)
        {
            for (int j = i; j < n.length; j++)
            {
                if (compare.compareItems(n[i],n[j]))
                {
                    temp = n[i];
                    n[i] = n[j];
                    n[j] = temp;
                }
            }
        }

        return n;
    }

    public Object[] sortInDescendingOrder(Object[] n)
    {
        for (int i = 0; i < n.length; i++)
        {
            for (int j = i + 1; j < n.length; j++)
            {
                if (!compare.compareItems(n[i],n[j]))
                {
                    temp = n[i];
                    n[i] = n[j];
                    n[j] = temp;
                }
            }
        }
        return n;
    }
}

public class Compare
{
    int a;
    int b;

    public Compare()
    {
        a = b = 0;
    }

    public boolean compareItems(Object item1, Object item2)
    {
        for (int i = 0; i < item1.toString().length() && i < item2.toString().length(); i++)
        {
            a = item1.toString().toLowerCase().charAt(i);
            b = item2.toString().toLowerCase().charAt(i);

            if (a > b)
            {
                return true;
            } else if (a < b)
            {
                return false;
            }
        }
        return true;
    }
}

最佳答案

您的作业有些不明确:

  1. 用户可以混合数字和字符串吗?
  2. 当你说数字时,这是否包括
    • 负数?
    • 任意长度的数字?
    • 分组分隔符(如 9,100)?
    • 小数分隔符(如 9.32 中所示)?
    • 科学计数法(如 9.34e+7)
    • SI 前缀(如 9K)
    • 十进制以外的数字系统(位置或其他)?
    • 特殊符号,如 ∞ ?

由于这是一项学校作业,我将假设任意长度的非负整数:-)

要提出算法,请考虑如何手动比较数字。最有可能的是,在检查两个数字都没有小数点后,您会比较它们的长度(数字越长越大),如果两个数字同样长,我们可以像字符串一样逐位比较它们。

在代码中,这看起来像这样:

boolean less(String a, String b) {
    if (isNumber(a)) {
        if (isNumber(b)) {
            return numericLess(a,b);
        } else {
            return true; // number before strings
        }
    } else {
        if (isNumber(b)) {
            return false; 
        } else {
            return alphabeticLess(a,b);
        }
    }
}

boolean numericLess(String a, String b) {
    if (a.length < b.length) {
        return true;
    } else if (a.length > b.length) {
        return false;
    } else {
        return alphabeticLess(a,b);
    }
}

关于java - Java 中的自定义比较器可以工作,但有一个缺陷,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46045327/

相关文章:

java - 如何在 Java 中格式化时间间隔?

java - 合并正则表达式中的两个条件

java - Android 数据保护程序关闭时获取 "BLOCKED"网络状态

java - 将比较器传递给 super 构造函数

c++ - 使用非默认比较谓词的集合容器

java - 在 Java 中实现 DAG 的不同方法

angular - 如何使用ngx-datatable实现服务器端分页+服务器端排序

python - 用 numpy 进行矢量化基数排序——它能打败 np.sort 吗?

python - 排序、打开和写入文件

java - 在 Comparator.comparing 中转换为可序列化