[Solved] Sorting an array of arrays

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
User avatar
Mr.Dandy
Posts: 466
Joined: Tue Dec 11, 2012 4:22 pm

[Solved] Sorting an array of arrays

Post by Mr.Dandy »

Me too,

This is my array of arrays:

Code: Select all

myTable = array( _
        array("max", "joe", "mel"),_
        array("foo", "kid", "jim"),_
        array("wil", "ron", "oxo"),_
        array("bar", "bob", "don"))
Somebody has a routine to sort it by column?

Thx
Last edited by Mr.Dandy on Fri Apr 29, 2016 10:17 am, edited 1 time in total.
OpenOffice 4.1.12 - Windows 10
User avatar
JohnSUN-Pensioner
Volunteer
Posts: 876
Joined: Fri Jan 14, 2011 1:21 pm
Location: Kyiv, Ukraine

Re: Sorting an array of arrays

Post by JohnSUN-Pensioner »

No, somebody hasn't. But can quickly write it

Code: Select all

Sub testSort
Dim myTable As Variant 
	myTable = array( _
        array("max", "joe", "mel"),_
        array("foo", "kid", "jim"),_
        array("wil", "ron", "oxo"),_
        array("bar", "bob", "don"))
		ShowArray(myTable, "Was")
		sortByColumn(myTable)
		ShowArray(myTable, "At now")
End Sub

Sub sortByColumn(SortList As Variant)
Dim s as Integer
Dim t as Integer
Dim i as Integer
Dim sortvalue1 As Variant
Dim sortvalue2 As Variant
	i = Ubound(SortList)
	For s = 1 to i 
		For t = 0 to i-s
			sortvalue1 = SortList(t)
			sortvalue2 = SortList(t+1)
			If CompareArray(sortvalue1, sortvalue2) > 0 Then                             
				SortList(t) = sortvalue2
				SortList(t+1) = sortvalue1
			End If
		Next t
	Next s 
End Sub

Function CompareArray(a1, a2) As Integer 
Dim i&
	For i = LBound(a1) To UBound(a1)
		If (a1(i) > a2(i)) Then
			CompareArray = 1
			Exit Function
		ElseIf (a1(i) < a2(i)) Then
			CompareArray = -1
			Exit Function
		EndIf
	Next i
	CompareArray = 0
End Function

Sub ShowArray(LocArray As Variant, Optional sTitle As String)
Dim i As Long 
Dim msgstring
	msgstring = ""
	For i = LBound(LocArray) to Ubound(LocArray)
		msgstring = msgstring + Join(LocArray(i),", ") + chr(13)
	Next
	Msgbox (msgstring, 0, sTitle)
End Sub
I may not have a lot to give but what I got I'll give to you...
Apache OpenOffice 4.1.5, LibreOffice 6.4.4.2 (x64) on Windows 7
If you think that I did not answer your question, make allowances for my imperfect English
User avatar
Mr.Dandy
Posts: 466
Joined: Tue Dec 11, 2012 4:22 pm

Re: Sorting an array of arrays

Post by Mr.Dandy »

Thanks JohnSUN
But I don't want to sort all columns
Just one and I would like to specify its index like:

Code: Select all

SortByColumn(myTable, 1)
And got this result:

Code: Select all

max	joe	mel
bar	bob	don
foo	kid	jim
wil	ron	oxo
Sorry for missing details
OpenOffice 4.1.12 - Windows 10
User avatar
JohnSUN-Pensioner
Volunteer
Posts: 876
Joined: Fri Jan 14, 2011 1:21 pm
Location: Kyiv, Ukraine

Re: Sorting an array of arrays

Post by JohnSUN-Pensioner »

No problem! Send this columnNumber to function Compare as parameter and verify only this elements
I may not have a lot to give but what I got I'll give to you...
Apache OpenOffice 4.1.5, LibreOffice 6.4.4.2 (x64) on Windows 7
If you think that I did not answer your question, make allowances for my imperfect English
Post Reply