Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Sorting an array

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Sorting an array

    I created an object called "Course" which has 4 instance variables - class code, instructor, title, and points. In the course class, I also have a compareTo method which compares the class code.

    In another class, I created an array of courses and I'm trying to sort them by the class code. How do I do that? I can't call the sort or compare to method on the array, and I'm kind of stuck...


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,302
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Sorting an array

    Three options. 1) write your own sort 2) Call Arrays.sort on the array 3) use a Collection, and call Collections.sort

  3. #3
    Junior Member
    Join Date
    Apr 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Sorting an array

    Thanks - the problem is, when I do courseListing.sort() it says I ca't invoke sort() on the array type course[]...

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,302
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Sorting an array


  5. #5
    Junior Member
    Join Date
    Apr 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Sorting an array

    One other question - is there a way to copy the contents of an array list into an array?

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,302
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Sorting an array

    Take a look at API..it is very good advice to become familiar them, how to read them and how to make use of them. There are copy operations provided in both Arrays (to copy arrays) and List (the interface ArrayList implements) to convert a List to an array
    List (Java Platform SE 6)

Similar Threads

  1. [SOLVED] Selection Sorting a text array
    By ComputerSaysNo in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 30th, 2011, 05:56 PM
  2. [SOLVED] Sorting an Array
    By petemyster in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 12th, 2010, 10:07 AM
  3. array sorting
    By herbisey in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 27th, 2010, 12:07 PM
  4. Sorting an Array
    By Prince_85 in forum Algorithms & Recursion
    Replies: 2
    Last Post: February 21st, 2010, 02:00 PM
  5. [SOLVED] Java program to sort arrays containing dates
    By scottyadam in forum Collections and Generics
    Replies: 1
    Last Post: March 9th, 2009, 06:08 PM

Tags for this Thread