Sort 2-dimensional array

  1. 6 days ago

    Beatrix W

    Jun 27 Testers, Third Party Store Europe (Germany)

    Before I reinvent the wheel: does anyone have a method to sort a 2-dimensional array? I have some database metadata where I can't use "order by" and need to sort the data before shoving it into a listbox.

    I did some Googling but I'm lazy.

  2. Jeff T

    Jun 27 Midlands of England, Europe

    There will probably be a better idea, but here's a quick and dirty idea..

    Flatten the 2D array to a 1D array by joining element 1 with element 2 as strings, split by (say) a ~ character

    Apple~23
    Apple~07
    Zoom~76
    Apple~01

    Sort the one-dimensional array using array.sort
    Apple~01
    Apple~07
    Apple~23
    Zoom~76

    Then fill your listbox using that, splitting as you go.
    Or even better, use TAB as the separator, and after the sort has happened, you can just Addrow into the listbox, and the tab will make the 2 parts hit the right columns automatically..

  3. Markus R

    Jun 28 Testers, Xojo Pro Europe / Germany / Lower Saxon...
    Edited 6 days ago

    you can make a sort method that return -1,0,1
    so you put something in and out comes a value

    in case of 2-dimensional array ... emm - i must test myself
    ok, the array.sort method is only for one dimension ...

    how about a class, you can store the instances into a array and you can sort this.
    inside of this class is a property array with a list of the other dimension, you can sort this easy with the sort method

    array.Sort(sortMethod As Delegate)

    so your 2 dimemsion array turns into a list with each entry have a list

  4. Beatrix W

    Jun 28 Testers, Third Party Store Europe (Germany)

    @Jeff Tullin: that sounds like a good idea.

    @Markus Rauch: haven't a clue what you mean.

  5. Eric B

    Jun 28 Testers, Xojo Pro Maryland, USA

    If the data is going into a ListBox anyway, then use the ListBox to do the sorting. Create an event handler ListBox.CompareRows to compare the data. Then put the data in using AddRow, and call ListBox.Sort to sort it.

  6. Jeff T

    Jun 28 Midlands of England, Europe

    call ListBox.Sort

    Again, it only sorts one column

    But if a hidden column holds the other columns concatenated together, you could sort on that.
    Probably need to hide the header..

  7. Kem T

    Jun 28 Testers, Xojo Pro, XDC Speakers, MVP Connecticut

    Within the CompareRows event, you can consider whatever columns or data you'd like. This is likely the quickest way to do it if you don't really need the original array sorted.

  8. Beatrix W

    Jun 28 Testers, Third Party Store Europe (Germany)

    @Eric Bloom: I did that. But I have a reload button. This gets new data from the server without the sort information. Looks quite odd.

    @Kem T : good idea. Doesn't work because I need the original data sorted so that the reload button works.

  9. Natascha C

    Jun 28 Testers, Xojo Pro Mödling, Lower-Austria, Austr...

    Load the data into an in-memory sqlite database, do your sorting work using GROUP BY and ORDER BY and then add the result to the listbox.

  10. Beatrix W

    Jun 28 Testers, Third Party Store Europe (Germany)

    @NataschaChrobok : that's going to be my last resort because it's really overkill.

  11. Kem T

    Jun 28 Testers, Xojo Pro, XDC Speakers, MVP Connecticut

    Can't you call Sort after reloading?

    Otherwise, go with Markus' suggestion. Create a class to hold the row data and create a single dimension array of that class. You can then sort the array through a Delegate.

  12. Rudolf J

    Jun 28 Germany Frankfurt

    Hello,

    Examble 3 column

    https://www.dropbox.com/s/7paj1w884bl31vj/Arraysort-dreispaltig.xojo_binary_project?dl=1

  13. Beatrix W

    Jun 28 Testers, Third Party Store Europe (Germany)

    @Rudolf J : thanks, that looks like Jeff Tullins suggestions.

    @Kem T : ah, that's what Markus Rauch meant. I'll try that.

  14. Natascha C

    Jun 28 Testers, Xojo Pro Mödling, Lower-Austria, Austr...

    @Beatrix Willius @Natascha Chrobok : that's going to be my last resort because it's really overkill.

    Is it? Programmingwise maybe. But databases are very fast with sorting. You might lose some time with importing and exporting the data. But as it is in memory, that might not make much difference. I'd be interested which approach is faster in the end.

  15. 5 days ago

    Markus R

    Jun 28 Testers, Xojo Pro Europe / Germany / Lower Saxon...

    @Beatrix W ah, that's what Markus Rauch meant. I'll try that.

    Denk Dir eine Liste mit Objekten wo bei jedes Objekt auch eine Liste haben kann.
    Jede Liste läßt sich über eine Vergleichsfunktion sortieren.
    Ob es Dir so nützt weiß ich nicht.

    Liste
      Obj1
          Liste
            A
            B
            C
      Obj2
          Liste
            X
            Y
            Z
  16. Norman P

    Jun 28 Testers, Xojo Pro outside admiring the sunshine,...

    There is also the issue of rearranging rows in a 2D array since this will require copying to swap the data in rows
    A db allows multi=key sorting really simply

  17. Philippe S

    Jun 28 Testers, Xojo Pro

    Or build an index with links to the original data positions (or keys). The index could be sorted as necessary. Then access to data through getters. Only worth it for smaller 100s of nb, else a sqlite db would do the job.
    Is it a very time sensitive function ?

  18. 4 days ago

    Thomas T

    Jun 30 Testers, Xojo Pro Europe (Germany, Munich)
    Edited 4 days ago

    Use Quicksort code written in Xojo that uses callbacks (or an Interface class), that asks you (a) to compare two values by their index, and (b) to swap two values by index. This separates the actually compared data from the the algorithm, which only needs to know which "item" is larger, without having to look at the data itself.

    Assuming the first dimension are the rows, and the 2nd dimension are the columns, first sort each column individually, once.
    When all cols are sorted, sort the rows.

    I have some code for this, and I had published it very long ago. It should be in my old RB archive at http://files.tempel.org/RB/ or at http://www.tempel.org/RB/ but I can't find it there right now...

    Ah, found it. Case still matters on the Web :) http://www.tempel.org/rb/ -- Search for "sort"

or Sign Up to reply!