如何按字母顺序对数组进行排序?

时间:2016-12-22 23:03:01

标签: java arrays sorting

我从Java教科书中获得了一项任务,按字母顺序对CD名称数组进行排序。作业说:

更改Tunes程序(清单6.7),以便它按照标题对CD进行排序。使用本章Sorts类中定义的常规对象排序。

这是清单6.7:

//********************************************************************
//  Tunes.java       Author: Lewis/Loftus/Cocking
//
//  Driver for demonstrating the use of an array of objects.
//********************************************************************

public class Tunes {
   //-----------------------------------------------------------------
   //  Creates a CDCollection object and adds some CDs to it. Prints
   //  reports on the status of the collection.
   //-----------------------------------------------------------------
   public static void main (String[] args) {
      CDCollection music = new CDCollection ();

      music.addCD ("By the Way", "Red Hot Chili Peppers", 14.95, 10);
      music.addCD ("Come On Over", "Shania Twain", 14.95, 16);
      music.addCD ("Soundtrack", "The Producers", 17.95, 33);
      music.addCD ("Play", "Jennifer Lopez", 13.90, 11);

      System.out.println (music);

      music.addCD ("Double Live", "Garth Brooks", 19.99, 26);
      music.addCD ("Greatest Hits", "Stone Temple Pilots", 15.95, 13);

      System.out.println (music);
   }
}

我不确定如何按字母顺序使用字母进行排序。我确实创建并将CD组织成一组数字,只是为了看它的外观:

//********************************************************************
//  Tunes.java       Author: Lewis/Loftus/Cocking
//
//  Driver for demonstrating the use of an array of objects.
//********************************************************************

public class Tunes {
   //-----------------------------------------------------------------
   //  Creates a CDCollection object and adds some CDs to it. Prints
   //  reports on the status of the collection.
   //-----------------------------------------------------------------
   public static void main (String[] args) {
      CDCollection[] music = new CDCollection[5];

      music[0] = new CDCollection ("By the Way", "Red Hot Chili Peppers", 14.95, 10);
      music[1] = new CDCollection ("Come On Over", "Shania Twain", 14.95, 16);
      music[2] = new CDCollection ("Soundtrack", "The Producers", 17.95, 33);
      music[3] = new CDCollection ("Play", "Jennifer Lopez", 13.90, 11);

      System.out.println (music);

      music[4] = new CDCollection ("Double Live", "Garth Brooks", 19.99, 26);
      music[5] = new CDCollection ("Greatest Hits", "Stone Temple Pilots", 15.95, 13);

      System.out.println (music);
   }
}

这是包含选择和插入排序方法的排序类:

//********************************************************************
//  Sorts.java       Author: Lewis/Loftus/Cocking
//
//  Demonstrates the selection sort and insertion sort algorithms,
//  as well as a generic object sort.
//********************************************************************

public class Sort {
   //-----------------------------------------------------------------
   //  Sorts the specified array of integers using the selection
   //  sort algorithm.
   //-----------------------------------------------------------------
   public static void selectionSort (int[] numbers) {
      int min, temp;

      for (int index = 0; index < numbers.length-1; index++) {
         min = index;
         for (int scan = index+1; scan < numbers.length; scan++)
            if (numbers[scan] < numbers[min])
               min = scan;

         // Swap the values
         temp = numbers[min];
         numbers[min] = numbers[index];
         numbers[index] = temp;
      }
   }

   //-----------------------------------------------------------------
   //  Sorts the specified array of integers using the insertion
   //  sort algorithm.
   //-----------------------------------------------------------------
   public static void insertionSort (int[] numbers) {
      for (int index = 1; index < numbers.length; index++) {
         int key = numbers[index];
         int position = index;

         // shift larger values to the right
         while (position > 0 && numbers[position-1] > key) {
            numbers[position] = numbers[position-1];
            position--;
         }

         numbers[position] = key;
      }
   }

   //-----------------------------------------------------------------
   //  Sorts the specified array of objects using the insertion
   //  sort algorithm.
   //-----------------------------------------------------------------
   public static void insertionSort (Comparable[] objects) {
      for (int index = 1; index < objects.length; index++) {
         Comparable key = objects[index];
         int position = index;

         // shift larger values to the right
         while (position > 0 && objects[position-1].compareTo(key) > 0) {
            objects[position] = objects[position-1];
            position--;
         }

         objects[position] = key;
      }
   }
}

我只是不确定如何使用字母而不是数字进行排序。提前感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

实现这一目标的方法是Sort#insertionSort(Comparable[])

String提供方法String#compareTo(String),因为它实现了接口Comparable<String>。创建此方法的方式是按字典顺序对字符串进行排序。

所以简单地将数组传递给Sort#insertionSort(Comparable[])应该可以解决问题。如何工作应在后面的书中解释(至少我希望如此)。

答案 1 :(得分:1)

您可以在compareTo方法中实现Comparable接口并比较Title,然后Collections.sort()将为您解决问题:

public class CDItem implements Comparable<CDItem>{
    String title;
    String singer;
    double price;

    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getSinger() {
        return singer;
    }
    public void setSinger(String singer) {
        this.singer = singer;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }

    CDItem(String title, String singer, double price){
        this.title=title;
        this.singer=singer;
        this.price=price;
    }

    @Override
    public int compareTo(CDItem item){
        return this.title.compareTo(item.title);
    }
}

public class CDCollection{
    List<CDItem> list=new ArrayList<CDItem>();

    public void addCD(String title, String singer, double price){
        CDItem item=new CDItem(title, singer, price);
        list.add(item);
    }

    public String toString(){
        String result="";
        for(int i=0; i<list.size(); i++){
            CDItem item=(CDItem)list.get(i);
            result=result + "|" + item.getTitle(); 
        }

        return result;
    }

    public void sort(){
        Collections.sort(list);
    }
}

然后在Tunes类中添加最后两行:

public class Tunes {
    public static void main(String[] args) {
        CDCollection music = new CDCollection ();

        music.addCD ("By the Way", "Red Hot Chili Peppers", 14.95);
        music.addCD ("Come On Over", "Shania Twain", 14.95);
        music.addCD ("Soundtrack", "The Producers", 17.95);
        music.addCD ("Play", "Jennifer Lopez", 13.90);

        System.out.println (music);

        music.addCD ("Double Live", "Garth Brooks", 19.99);
        music.addCD ("Greatest Hits", "Stone Temple Pilots", 15.95);

        System.out.println (music);

        music.sort();
        System.out.println (music);
    }   
}