I need to sort these strings in order and I have changed the code of my program which use to sort integers and now I need it to sort strings and I cannot get it to work, any help.....

import java.util.Scanner;

public class Strings {


// --------------------------------------------
// Reads in an array of strings, sorts them,
// then prints them in sorted order.
// --------------------------------------------
public static void main(String[] args) {

    stringList = new String;
    String size;
            Comparable temp;

    Scanner scan = new Scanner(System.in);

    System.out.print("\nHow many strings do you want to sort? ");
    size = scan.nextInt();
    StringList = new String[size];

    System.out.println("\nEnter the string(s)...");
    for (String i = 0; i < size; i++)
        StringList[i] = scan.nextInt();
    Sorting.insertionSort(StringList);

    System.out.println("\nYour strings in sorted order...");
    for (String i = 0; i < size; i++)
        System.out.print(StringList[i] + " ");

        System.out.println();
    }
}

Recommended Answers

All 2 Replies

If you want to sort an array of Strings, you can do the following:

// An example array of Strings
String[] myArray = new String[] {"orange", "banana", "apple", "melon", "grape"}
java.util.Arrays.sort(myArray);

I could see some compilation problems here at lines 12 and 20.
There is also another missing piece:

Sorting.insertionSort(StringList);

Can you give the details of this class Sorting which is having the method insertionSort(String[] arrayOfStrs)

Using this information we can find what sorting algorithm is there and fix the bugs if any

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.