【问题标题】:Sorting objects in alphabetical order按字母顺序对对象进行排序
【发布时间】:2013-09-18 02:03:03
【问题描述】:

对于我的任务,我必须创建一个方法来对存储在对象类中的整数和字符串进行排序。请记住,我必须使用演员表。我想使用泛型,但我的老师坚持使用 1.4.2(没有泛型)。我可以对时间进行排序,对于字母排序,我使用我的方法对时间进行排序并添加了一个 compareTo。我玩了一下,但是当我输出它时,它会按照我输入的顺序给我输入的所有内容。不按字母顺序。

这是我创建的用于存储输入的类:

public class showInfo 
{
    String name;
    String day; 
    int time;    
}

以下是按名称排序的方法!

//method to sort and display info
public static void sortName(){          
    for(int i = 0; i < show.size() - 1; i++) {
        for(int j = 0; j < show.size() - 1; j++){
            if(((showInfo)show.get(i)).name.compareTo(((showInfo)show.get(i+1)).name) > 0){
                showInfo temp = new showInfo();
                temp.name = ((showInfo)show.get(j)).name;
                temp.day = ((showInfo)show.get(j)).day;
                temp.time = ((showInfo)show.get(j)).time;

                ((showInfo)show.get(j)).time = ((showInfo)show.get(i)).time;
                ((showInfo)show.get(j)).day = ((showInfo)show.get(i)).day;
                ((showInfo)show.get(j)).name = ((showInfo)show.get(i)).name;

                ((showInfo)show.get(i)).time = temp.time;
                ((showInfo)show.get(i)).day = temp.day;
                ((showInfo)show.get(i)).name = temp.name;
            }
        } 
    } 

任何帮助都会很棒!提前致谢。 :)

(PS。我知道我需要将“showInfo”更改为“ShowInfo”,但我会在完成后进行。)

【问题讨论】:

标签: java sorting


【解决方案1】:

您的代码的一个问题是您将show.get(i)show.get(i+1) 进行比较,但随后将show.get(i)show.get(j) 进行了比较。您应该与show.get(j) 进行比较。此外,内部循环应该转到j &lt; show.size() 而不是show.size() - 1。最后,您可以在i + 1 而不是0 开始内循环。

一旦确定需要交换,您可以通过简单地交换列表中的引用而不是交换每个字段来做得更好:

showInfo tmp = (showInfo)show.get(i);
show.set(i, show.get(j));
show.set(j, tmp);

【讨论】:

    【解决方案2】:

    我假设showList,您必须按name 排序。

    首先,让showInfo实现Comparable

    public class showInfo implements Comparable
    {
        String name;
        String day; 
        int time;
    
        public int compareTo(Object o)
        {
            showInfo other = (showInfo) o;
            return name.compareTo(other.name);
        }  
    }
    

    然后,在列表中使用`Collections.sort()'

    Collections.sort(show);
    

    【讨论】:

    • 如果不能修改sh​​owInfo也可以写一个Comparator
    • 既然听起来 OP 必须以多种不同的方式进行排序,那么写多个 Comparators 不是更有意义吗?
    • 这是他们老师的作业,他们几乎肯定需要自己实现排序逻辑,而不是依赖Collections.sort。想知道是实现Comparable 还是写Comparators 更合适是没有实际意义的。
    • 因为懒惰,我总是会采取最简单的方法来做某事,除非另有说明(在这种情况下并非如此)。
    【解决方案3】:

    你可以这样做......

    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.List;
    
    class myComparator implements Comparator {
    
        public int compare(Object o1, Object o2) {
            return ((o1.toString().charAt(0) > o2.toString().charAt(0)) ? 1 : (o1
                    .toString().charAt(0) == o2.toString().charAt(0)) ? 0 : -1);
        }
    }
    
    public class Sample {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            List l = new ArrayList();
            l.add("hello");
            l.add("abc");
            l.add("World");
            l.add("hi");
    
            System.out.println("Before sorting");
            for (Object i : l) {
                System.out.println(i.toString());
            }
    
            Collections.sort(l, new myComparator());
    
            System.out.println("After sorting");
            for (Object i : l) {
                System.out.println(i.toString());
            }
    
        }
    
    }
    

    【讨论】:

      【解决方案4】:

      您在这里错误地使用了i

      if(((showInfo)show.get(i)).name.compareTo(((showInfo)show.get(i+1)).name) > 0){
      

      我认为第二个i应该是j来实现冒泡排序

      if(((showInfo)show.get(i)).name.compareTo(((showInfo)show.get(j+1)).name) > 0){ 
      

      【讨论】:

        【解决方案5】:

        不确定这是否是您要寻找的,它使用 casting 而不是 generics,无论如何,我希望这会有所帮助

        pastebin

        【讨论】:

          猜你喜欢
          • 2013-10-28
          • 2019-06-15
          • 2011-08-29
          • 2014-12-30
          • 2021-04-23
          • 1970-01-01
          • 2023-01-30
          • 2018-12-03
          • 2017-05-22
          相关资源
          最近更新 更多