Some array sorting

Not exactly sure how to implement this.

Page 1 of 1

4 Replies - 4568 Views - Last Post: 06 April 2007 - 02:14 AM Rate Topic: -----

#1 Red8679  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 20-February 07

Some array sorting

Posted 02 April 2007 - 06:51 PM

So I've been searching the api and trying who knows how many different ways to do this, but I can't get an array of strings to sort in alphabetical order. There's more to this whole program, but just to get the idea down:

import java.lang.*;
import java.util.*;

public class arraySort{

public static void main(String[] args)
throws java.io.IOException{

   InputStreamReader isr = new InputStreamReader(System.in);
   BufferedReader br = new BufferedReader(isr);

   String names[] = new String[10];
   int i = 0;

   for(i = 0; i < 10; i++){
	  System.out.print("Enter a name: ");
	  names[i] = br.readLine();
	  }

/************************************************/
/*Here is where I want to sort them in alphabetical order. */
/************************************************/

   for(i = 0; i < 10; i++)
	  System.out.println(names[i]);

   }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Some array sorting

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Some array sorting

Posted 02 April 2007 - 06:55 PM

I might be tempted to implement it as an Array object, and use the sort() method.

http://java.sun.com/...til/Arrays.html

http://leepoint.net/.../70sorting.html

http://www.java2s.co...ayofStrings.htm
Was This Post Helpful? 0
  • +
  • -

#3 GWatt  Icon User is offline

  • member icon

Reputation: 270
  • View blog
  • Posts: 3,068
  • Joined: 01-December 05

Re: Some array sorting

Posted 02 April 2007 - 07:21 PM

You can have a String array, and use the Arrays.sort(Object[] a) method. It sorts based on natural value, which for Strings is the lexical, or alphabetical order.
Was This Post Helpful? 0
  • +
  • -

#4 mprince786  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 05-April 07

Re: Some array sorting

Posted 05 April 2007 - 07:26 PM

Why dont you give buble sort a try in your code or some other algoritm :pirate:
Was This Post Helpful? 0
  • +
  • -

#5 3ks7rim3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 06-April 07

Re: Some array sorting

Posted 06 April 2007 - 02:14 AM

View PostGWatt, on 2 Apr, 2007 - 07:21 PM, said:

You can have a String array, and use the Arrays.sort(Object[] a) method. It sorts based on natural value, which for Strings is the lexical, or alphabetical order.

Bubble sort method is slow! Use the quick sort
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1