Android Question Sorting 3 arrays using QuickSort

Discussion in 'Android Questions' started by wonder, Mar 24, 2015.

  1. wonder

    wonder Expert Licensed User

    Hi guys,

    I was wondering if anyone could help me. This shouldn't be very difficult to achieve, but since I'm back to work this week, I don't have much time to code...

    Anyway, here it goes:
    I have 3 arrays of size n. For simplification purposes, n = 6.
    Dim a(6as Float
    Dim b(6as Float
    Dim c(6as Float
    Let's use these values as example.

    Now, what I need is a B4A algorithm which sorts (descending) array C using Quicksort.
    I want these arrays to be "linked horizontally", so that both A and B arrays are also sorted according to the values of C.

    This isn't related to databases at all, so using MySQL commands won't work.
    Also, I specifically need this to be done with arrays, I don't want to use lists.
  2. Troberg

    Troberg Well-Known Member Licensed User

    My first suggestion would be to use one array, and fill it with a type that contains the values.

    Type Row(A as float, B as float, C as float)
    Dim Rows(6) as Row

    Then you can write a quicksort to sort that array based on Rows(x).C.

    The other, less elegant way, is simply to sort the C array, and whenever you move something in that array, you also move it in the other two arrays.
    wonder likes this.
  3. Erel

    Erel Administrator Staff Member Licensed User

    I agree. You need to create a type that holds the three values. You can then use List.SortType to sort it.
    Peter Simpson and wonder like this.
  4. wonder

    wonder Expert Licensed User

    Thank you Troberg and Erel. Is it possible to find an example somewhere?
  5. Erel

    Erel Administrator Staff Member Licensed User

  6. wonder

    wonder Expert Licensed User

    Thank you! I got it working! :)
  7. sorex

    sorex Expert Licensed User

    this one is rather simple but on more complex things you could insert it to some temp SQLite table and pull it back in with the different sorting methods (or other actions) you require.
    jsanchezc likes this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice