how do i sort an array?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 9 of 9

Thread: how do i sort an array?

  1. #1
    Join Date
    Feb 2005
    Posts
    4

    how do i sort an array?

    I have the following code and I want to be able to sort the column [r][2] into ascending order, I have the sort array bit but it doesn’t work, how is it done?

    Replies much appreciated, thank you


    import java.awt.*;
    import java.util.*;

    public class students
    {
    public static void main(String args[])
    {


    int [][] students = new int [10][3];


    for(int r=0;r<10;r++)
    {
    for(int c=0;c<2;c++)
    {
    students[r][c]=(int)(Math.random()*70+10);
    students[r][2]=((students[r][0]+students[r][1])/2);
    }
    System.out.println("Exam = "+students[r][0]+"%");
    System.out.println("Coursework = "+students[r][1]+"%");
    System.out.println(" ");

    Arrays.sort(students[r][2]);

    System.out.println("Average mark is "+students[r][2]+"%");

    System.out.println("- - - - -");

  2. #2
    Join Date
    Feb 2005
    Posts
    5
    traverse the array once for each element you want to sort, keep a variable to hold the lowest value so far and add it to a new array at the end of the traversal. also keep an array to keep track of which array elements youve already put in the new array so u dont get the same element over and over

  3. #3
    Join Date
    Feb 2005
    Posts
    4
    i dont understand all i wnat to do at the ned is to know which is the highest number int he 3rd column and the lowest, is there an easier way to do this?

  4. #4
    Join Date
    Feb 2005
    Posts
    5
    well just loop thru the values and keep a variable for highest and lowest,
    at each value put

    if(value>highest)
    {
    highest = value;
    }

    and

    if(value<lowest)
    {
    lowest = value;
    }

    and by the end ul hav the highest and lowest values stored

  5. #5
    Join Date
    Feb 2005
    Posts
    4
    how would i no what the values are they are randomly generated?

  6. #6
    Join Date
    Feb 2005
    Posts
    2

    rsdfeppa sdfs #$#1s

    $#23 324@#6wdsd$%#v35
    C$%4534v35
    C34
    c34c#$%65645^$!16FF

  7. #7
    Join Date
    Feb 2005
    Posts
    2

    #$%&^&

    $@4B%N367n37n37#$n3taert5
    #$%#$%VQ#$%&^&*HJGFHJ&
    %^&#%^&N$%&RFDGSav aeR
    Attached Images Attached Images

  8. #8
    Join Date
    Feb 2005
    Posts
    4
    ?????

  9. #9
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    Cool Your code is a bit mysterious

    If you want to use the sort method supplied bu the Collections class you
    must implement the Comparator interface in the class that you wish
    to sort. It can be implemented in any class you choose though, but then the
    equals() method specified by the interface may not be 100% ok....

    I am having a problem seeing what it is you want to sort; is it a specific
    column ? The statement:

    ...sort(students[r][2]);

    is meaningless if students is a 2-dimensional array.

    If you could clarify what you want to achieve I can show you how to do this.
    Anyway, here is a simple example of using the Collections.sort() method:
    Code:
    import java.util.*;
    
    /**
     * A simple example of sorting.  It sorts a pack of dogs
     * according to race and name.  Dogs of same race are sorted
     * by their names
     * @author sjalle
     * @version 1.0
     */
    
    class Dog implements Comparator {
      String name=null;
      String race=null;
      public Dog () {}
      public Dog(String name, String race) {
        this.name=name;
        this.race=race;
      }
      /**
       * Comparator implementation, used for sorting
       * @param o1
       * @param o2
       * @return
       */
      public int compare(Object o1, Object o2) {
        Dog dog1=(Dog)o1;
        Dog dog2=(Dog)o2;
        if (dog1.equals(dog2)) return 0;
        if (dog1.race.equals(dog2.race)) {
          // same race, different names, so the names defines
          // the sort order for these two dogs
          return dog1.name.compareTo(dog2.name);
        } else {
          // sort by race (that didn't sound quite right...)
          return dog1.race.compareTo(dog2.race);
        }
      }
      /**
       * Comparator implementation, used for equality testing
       * @param obj
       * @return
       */
      public boolean equals(Object obj) {
        if (!(obj instanceof Dog)) return false; // not even a Dog...
        Dog aDog=(Dog)obj;
        return this.name.equals(aDog.name) && this.race.equals(aDog.race);
      }
    }
    public class SortDogs {
      public static void main(String [] args) {
        ArrayList aList = new ArrayList();
        aList.add(new Dog("SnobbermanPincher","Jaws"));
        // etc.
        // assume this arrayList is full of dogs now...
        Dog aDog=new Dog(); // just used for the Comparator interface
        Collections.sort(aList, aDog);
        Dog [] dogs=(Dog[])aList.toArray();
      }
    }
    Last edited by sjalle; 02-25-2005 at 09:21 AM.
    eschew obfuscation

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center