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 4 of 4

Thread: Array Sort

  1. #1
    Member
    Join Date
    Sep 2018
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Array Sort

    Hello,
    could I ask for an example of a sort for integers in ascending order in an Array?
    I was trying to sort my Array this morning but it wasn't working.
    I know that usually everyone wants to see the code, but I'm at work, so I don't have the code here. So if someone could please provide a generic example, I'd really appreciate it.

  2. #2
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Array Sort

    One way to sort a list is to start with two loops. The inner loop compares to what the outer loop is pointing to and swaps the two if necessary. Once the inner loop gets to the end, it starts over and the outer loop goes to the next element in the list. This is known as a bubble sort. It is considered a poor sort in terms of efficiency but is useful for teaching newcomers to programming.

    Note that you can also google sorting and get lots of examples.

    Regards,
    Jim

  3. #3
    Member
    Join Date
    Sep 2018
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Array Sort

    Thank you for the reply jim829. I actually did google how to sort, but it wasn't working, which is why I was asking for a generic example. I'll try googleing a bubble sort

  4. #4
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    277
    My Mood
    Amused
    Thanks
    8
    Thanked 19 Times in 19 Posts

    Default Re: Array Sort

    Arrays.sort() will do the trick.

    Example
    int arr[]={33,3,4,5,1};
    Arrays.sort(arr);
    System.out.println(Arrays.toString (arr));
    Whatever you are, be a good one

Similar Threads

  1. How do you sort an array? [using bubble sort]
    By namenamename in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 27th, 2013, 03:45 PM
  2. Replies: 2
    Last Post: November 11th, 2012, 10:44 PM
  3. array bubble sort
    By Olympaphibian89 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 1st, 2012, 05:53 PM
  4. How to call a C sort function to sort a Java Array.
    By Dwere13 in forum Java Native Interface
    Replies: 22
    Last Post: July 12th, 2012, 04:44 PM
  5. can't figure out how to sort an array.
    By etidd in forum Java Theory & Questions
    Replies: 2
    Last Post: February 6th, 2010, 03:07 PM