sort array

PT_PT_
edited June 2014 in Programming Questions

heey, i've made a game and i want to sort an array on the score (example: a[] = {5,9,2,6,3,2} = {2,2,3,5,6,9}) but then i wanted to sort another array on the same way as the first example a[ ] = {2,6,5,8} --> {2,5,6,8} b[ ] = {6,9,2,5} --> {6,2,9,5} yes? how can i do it?

Tagged:

Answers

  • You should make a class having both values, instead of separate arrays. See From several arrays to classes.
    Thus, correlated values are kept together, and you can define on which field you do the sorting. Search for Comparator or Comparable from the main site.

  • Don't forget Processing got a function to sort arrays called sort():
    http://processing.org/reference/sort_.html

  • GoToLoop, the problem isn't to sort arrays, the problem is to apply the sorting of one array to another.

  • _vk_vk
    edited June 2014

    There is this article:

    http://wiki.processing.org/w/Sorting_with_Comparable

    And I have this old test I made once:

    import java.util.*;
    int[] xSizes = new int[5];
    int[] ySizes = new int[5];
    int xPos;
    PFont f;
    Test[] tests = new Test[5];
    
    void setup()
    {
      size(800, 408);
      xSizes[0] = 140; 
      xSizes[1] = 80; 
      xSizes[2] = 200;
      xSizes[3] = 100;
      xSizes[4] = 85;
      ySizes[0] = 70;
      ySizes[1] = 100;
      ySizes[2] = 40;
      ySizes[3] = 40;
      ySizes[4] = 85;
    
      for (int i=0 ; i<tests.length ; i++)
      {
        tests[i] = new Test(xSizes[i], ySizes[i]);
      }
      f = createFont("arial", 18);
      stroke(50);
    
      background(70);
    }
    
    
    void draw()
    {
      // Draw first without sorting
      xPos = 0;
      fill(255);
      for (int i=0 ; i<tests.length ; i++)
      {
        tests[i].display(xPos, 0);
        xPos = xPos + tests[i].xSz;
      } 
      fill(0);
      text("no sorting", 10, 10);
    
      // sort by x size
      Arrays.sort(tests, new xSzComparator());
    
      xPos = 0;
      fill(255);
      for (int i=0 ; i<tests.length ; i++)
      {
        tests[i].display(xPos, 102);
        xPos = xPos + tests[i].xSz;
      } 
    
       fill(0);
      text("sorted by width (szX)", 10, 115);
    
    
      // sort by y size
      Arrays.sort(tests, new ySzComparator());
    
      xPos = 0;
      fill(255);
      for (int i=0 ; i<tests.length ; i++)
      {
        tests[i].display(xPos, 204);
        xPos = xPos + tests[i].xSz;
      }
    
       fill(0);
      text("sorted by height (szY)", 10, 220);
    
      //sort by "natural order" implemented with compareTo()
      // here if y size is equal bigger x sizes come first...
      Arrays.sort(tests);
    
      xPos = 0;
      fill(255);
      for (int i=0 ; i<tests.length ; i++)
      {
        tests[i].display(xPos, 308);
        xPos = xPos + tests[i].xSz;
      }
      fill(0);
      text("sorted by \"natural order\" \n if height equals, bigger width come first", 10, 320); 
      noLoop();
    }
    
    
    
    
    //// class Test
    
    
    class Test implements Comparable
    { 
      int xSz;
      int ySz;
    
      Test (int _xSz, int _ySz)
      {
        xSz =_xSz;
        ySz =_ySz;
      }
    
    
    
      void display(int x, int y)
      {
        rect(x, y, xSz, ySz);
      }
    
      /// setting the natural order
      /// not need to use x thought 
      // i did to test comparing by this and then by that
      int compareTo(Object o)
      {
        Test other=(Test)o;
    
        if (other.ySz > ySz) 
        { 
          return -1;
        }
        else if (other.ySz == ySz && other.xSz < xSz) // then.. 
        {
          return -1;
        }
        else if (other.ySz == ySz && other.xSz == xSz)
        {
          return 0;
        }
        else 
        {
          return 1;
        }
      }
    }
    
    
    //// end of Test class
    
    
    
    class xSzComparator implements Comparator {
    
      public int compare(Object test1, Object test2) {    
    
        //parameter are of type Object, so we have to downcast it to Test objects
    
        int test1xSz = ((Test)test1).xSz; 
        int test2xSz = ((Test)test2).xSz;
    
        if (test1xSz > test2xSz)
          return 1;
        else if (test1xSz < test2xSz)
          return -1;
        else
          return 0;
      }
    }
    
    
    class ySzComparator implements Comparator {
    
      public int compare(Object test1, Object test2) {    
    
        //parameter are of type Object, so we have to downcast it to Test objects
    
        int test1ySz = ((Test)test1).ySz;        
        int test2ySz = ((Test)test2).ySz;
    
        if (test1ySz > test2ySz)
          return 1;
        else if (test1ySz < test2ySz)
          return -1;
        else
          return 0;
      }
    }
    
  • edited June 2014

    b[ ] = {6,9,2,5} --> {6,2,9,5} yes?

    I don't think sequence {6,2,9,5} is an ordered list! 8-}
    Nevertheless, I did an attempt as well:

    // forum.processing.org/two/discussion/5826/sort-array
    
    import java.util.Arrays;
    import java.util.List;
    
    final List<byte[]> scoreLists = new ArrayList();
    
    void setup() {
      scoreLists.add(new byte[] {
        5, 9, 2, 6, 3, 2
      }
      );
      scoreLists.add(new byte[] {
        2, 6, 5, 8
      }
      );
      scoreLists.add(new byte[] {
        6, 9, 2, 5
      }
      );
    
      println("Unsorted Score Tables:\n");
      displayLists(scoreLists);
    
      println("Sorted Score Tables:\n");
      sortLists(scoreLists);
      displayLists(scoreLists);
    
      exit();
    }
    
    void displayLists(List<byte[]> lists) {
      int num = 0;
      for (byte[] scores: lists) {
        println("List: #" + nf(num++, 2) + ":");
        for (byte score: scores)  print(score + ", ");
        println("\n");
      }
    }
    
    void sortLists(List<byte[]> lists) {
      for (byte[] scores: lists)  Arrays.sort(scores);
    }
    
  • _vk_vk
    edited June 2014

    a[ ] = {2,6,5,8} --> {2,5,6,8} //<- this gets sorted[

    b[ ] = {6,9,2,5} --> {6,2,9,5} //< - but this don't.

    There is a relationship that is kept:

    a 2 b 6

    a 6 b 9

    a 5 b 2

    a 8 b 5

    I think this is why PhiLho suggested the object with a natural order of sorting.

    import java.util.Arrays;
    
    NumberPair[] pairs = new NumberPair[4];
    
    void setup(){
      pairs[0] = new NumberPair (2, 6, 0);
      pairs[1] = new NumberPair (6, 9, 1);
      pairs[2] = new NumberPair (5, 2, 2);
      pairs[3] = new NumberPair (8, 5, 3);
    
    
      println(pairs);
      Arrays.sort(pairs);
      println("\nsorted:");
      println(pairs);
    
    
    }
    
    
    
    
    
    
    //// class Test
    
    
    class NumberPair implements Comparable
    { 
      int a;
      int b;
      int i;
    
      NumberPair (int _a, int _b, int _i)
      {
        a =_a;
        b =_b;
        i = _i;
      }
    
    
    
      String toString()
      {
        return " #" + i + " a = " + a + " / b = " + b;
      }
    
      /// setting the natural order
      int compareTo(Object o)
      {
        NumberPair other = (NumberPair)o;
    
        if (other.a > a) 
        { 
          return -1;
        }
        else if (other.a == a) // then.. 
        {
          return 0;
        }
        else 
        {
          return 1;
        }
      }
    }
    
Sign In or Register to comment.