• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Sorting an array list into numerical order - help please!!

 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I've written a short class with an int array, which produces a list in the order they were added to the table. The list is taken from a text file with 10,000 numbers.

My problem is that I want to be able to keep the array in numerical order, and currently I have no idea how to achieve this.

My code is currently as follows:



Any help would really be appreciated!!
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To keep your array sorted, you can try Arrays.sort(yourArrayName) method in Arrays.For more info look at this doc
 
celine scarlett
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Anybody know how I might achieve a sort of an array list without using the 'Arrays' class. For example, possibly modifying the 'add' method in the existing code?

Thanks!
 
Sunny Kumar
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by celine scarlett:
Hi,

Anybody know how I might achieve a sort of an array list without using the 'Arrays' class. For example, possibly modifying the 'add' method in the existing code?

Thanks!



If you want to manaully sort your array then you can try bubble sort coding. However calling it everytime after adding a new element can be a little expensive in terms of performance if your array is quite large or changing frequently.
 
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you have to use an array, and you cannot use the Arrays class to sort them, you will have to implement your own sorting algorithm. Do a web search on sorting algorithms. A bubble sort will be the simplest to implement and should work fine for the limited number of items you have.

If you are not limited to arrays, you might check out the SortedSet class in the Java API. You'll have to wrap your int values in Interger objects before adding them to the set.

Cheers,
 
celine scarlett
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

That's great. Thanks for all the help. I shall persevere with this problem tomorrow, and hopefully be able to report some success.

Many thanks.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic