[英]Insert sorting strings from an array in alphabetical order
I am really not 100% sure how to insert sort alphabetically from an array. 我真的不是100%知道如何从数组中按字母顺序插入排序。 This is what I have so far and any help is appreciated.
到目前为止,这是我所拥有的,我们将为您提供任何帮助。
I am trying to use the insert sort algorithm to sort alphabetically in this project. 我正在尝试使用插入排序算法在此项目中按字母顺序排序。
I am getting some errors in sorting, as well as runtime errors. 我在排序时遇到一些错误,以及运行时错误。
Thanks! 谢谢!
// The "Insertion_Sort_Example" class.
import java.awt.*;
import java.io.*;
import java.util.*;
public class SortPlanets
{
public static void main (int [] args)
{
String Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune, Pluto ;
String list [] = {Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune, Pluto}; // Array holding contents
System.out.println ("Array contents before sorting..."); // simple print statements to show proof of before sort
for(int i = 0; i < 5; i++) {
System.out.println(list[i]);
}
System.out.println ("");
System.out.println ("************************************");
insertSort (list); // call to insert function
System.out.println ("************************************"); // insert after
System.out.println ("Array contents after sorting...");
for(int i = 0; i < 5; i++) {
System.out.println(list[i]);
}
// Place your program here. 'c' is the output console
} // main method
public static void insertSort (String [] list) // sort function
{
for (int top = 1 ; top < list.length ; top++)
{
int item = list [top];
int i = top;
while (i > 0 && item < list [i - 1])
{
list [i] = list [i - 1];
i--;
}
list [i] = item;
System.out.print (list [0]);
System.out.print (" ");
System.out.print (list [1]);
System.out.print (" ");
System.out.print (list [2]);
System.out.print (" ");
System.out.print (list [3]);
System.out.print (" ");
System.out.print (list [4]);
System.out.println ();
}
}
} // Insertion_Sort_Example class
java.util.Collections.sort(list);
This will put the list in alphabetical order 这会将列表按字母顺序排列
EDIT: Atish has the more complete answer 编辑:Atish有更完整的答案
String list [] = {Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune, Pluto};
Sort your list list
using java.util.Collections
method 使用
java.util.Collections
方法对列表list
进行排序
java.util.Collections.sort(list);
sort()
api sort the array in Natural Ordering (sort alphabetically)
. sort()
api以Natural Ordering (sort alphabetically)
对数组进行Natural Ordering (sort alphabetically)
。
I just cleaned up your code so that it works -- here you go: 我刚刚清理了您的代码,使其可以正常工作-在这里,您可以进行以下操作:
A few things I noticed -- When you initialize your planets -- make sure that you initialize them so they are not all null eg String Earth = "Earth", not just String Earth. 我注意到的几件事-初始化行星时,请确保对其进行初始化,以使它们都不都是空的,例如String Earth =“ Earth”,而不仅仅是String Earth。 When you compare strings in your sort - make sure you use .compareTo() method.
在比较排序中的字符串时-确保使用.compareTo()方法。 You cannot use < or > to compare String objects.
您不能使用<或>比较String对象。 Other than that you were kinda close.
除此之外,您还很亲密。 GJ mate.
GJ队友。
package com.cudp.cuprodigy.utils;
import java.awt.*;
import java.io.*;
import java.util.*;
public class SortPlanets
{
public static void main (String [] args)
{
String Mercury="Mercury", Venus="Venus", Earth="Earth", Mars="Mars", Jupiter="Jupiter", Saturn="Saturn", Uranus="Uranus", Neptune="Neptune", Pluto="Pluto" ;
String list [] = {Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune, Pluto};
System.out.println ("Array contents before sorting...");
System.out.print (list [0]);
System.out.print (" ");
System.out.print (list [1]);
System.out.print (" ");
System.out.print (list [2]);
System.out.print (" ");
System.out.print (list [3]);
System.out.print (" ");
System.out.print (list [4]);
System.out.println ("");
System.out.println ("************************************");
System.out.println ("PLEASE. NOT THE ASS.");
SortPlanets.insertSort (list);
System.out.println ("************************************");
System.out.println ("Array contents after sorting...");
System.out.print (list [0]);
System.out.print (" ");
System.out.print (list [1]);
System.out.print (" ");
System.out.print (list [2]);
System.out.print (" ");
System.out.print (list [3]);
System.out.print (" ");
System.out.print (list [4]);
// Place your program here. 'c' is the output console
} // main method
public static void insertSort (String [] list)
{
for (int top = 1 ; top < list.length ; top++)
{
String item = list [top];
int i = top;
while (i > 0 && item.compareTo(list [i - 1]) < 0)
{
list [i] = list [i - 1];
i--;
}
list [i] = item;
System.out.print (list [0]);
System.out.print (" ");
System.out.print (list [1]);
System.out.print (" ");
System.out.print (list [2]);
System.out.print (" ");
System.out.print (list [3]);
System.out.print (" ");
System.out.print (list [4]);
System.out.println ();
}
}
} // Insertion_Sort_Example class
Try Below code: 尝试以下代码:
import java.util.Arrays;
public class Mainclass {
public static void main(String[] args) {
String list [] = { "Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto"};
Arrays.sort(list);
for(String str:list)
System.out.println(str);
}
}
I believe that the answer from atish is good and easy to understand. 我相信atish的答案很好并且易于理解。 But, there is an alternative if you can consider using the Java libraries for sorting.
但是,如果您可以考虑使用Java库进行排序,则还有另一种选择。
Java 8 introduces the stream APIs that has natural built in sorting. Java 8引入了具有自然内置排序功能的流API。 The
sorted
-method can be invoked as seen below or by providing a custom Comparator
. 可以如下所示或通过提供自定义
Comparator
来调用已sorted
方法。
// Array of planet names (is Pluto technically a planet?)
String[] planets = {"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto"};
// Java 8 Streams are used here to do the sorting and iterations
String[] sortedPlanets = Arrays.stream(planets) // Create the stream
.sorted() // Sort the stream
.toArray(String[]::new); // Convert back to String[]
System.out.println(Arrays.toString(planets)); // order from the sun
System.out.println(Arrays.toString(sortedPlanets)); order alphabetically
Just a few lines of streaming Java 8 code does the trick! 只需几行Java 8流代码就可以解决问题!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.