A more complete version which allows use of some other XSortable options - descending, sort by columns
Code: Select all
REM ***** BASIC *****
sub testsortarray2
'TEST PREFIXES LENGTH OF STRING AND COLUMN MARKIER TO STRINGS,
'SORTS BY LENGTH IN ASCENDING ORDER (COLUMN1)
'AND THEN SORTS ORIGINAL STRING ((COLUMN2) IN DESCENDING ALPHANUMERIC ORDER
t= array("zebra","bats","aadvark","ant","goose","dog","cat","canary")
for i = 0 to ubound(t)
t(i) =len(t(i)) & chr(9) & t(i)
next
'array,delimeter,IsSortAscending0, IsSortNumeric0, SortRowOrColumnNo0
'IsSortAscending1, IsSortNumeric1, SortRowOrColumnNo1
'IsSortAscending2, IsSortNumeric2, SortRowOrColumnNo2)
t=sortarray2(t,chr(9),,true,1,false,,2)
for i = 0 to ubound(t)
st =st & t(i) & chr(10)
next
msgbox st
end sub
function sortArray2(byval arr,optional delimeter as string,optional IsSortAscending0 as boolean,optional IsSortNumeric0,optional SortRowOrColumnNo0 as integer, _
optional IsSortAscending1 as boolean,optional IsSortNumeric1,optional SortRowOrColumnNo1 as integer, _
optional IsSortAscending2 as boolean,optional IsSortNumeric2,optional SortRowOrColumnNo2 as integer)
Dim mArgs(0) as New com.sun.star.beans.PropertyValue
dim doc,otext,i as long,sd,en,tc
mArgs(0).name= "Hidden"
mArgs(0).value = true
Doc=StarDesktop.loadComponentFromURL("private:factory/swriter","_blank",0,mArgs())
otext = doc.text
otext.string = ""
tc =otext.createtextcursorbyrange(otext.getstart)
for i= 0 to ubound(arr)
tc.string =arr(i)
tc.collapsetoend
if i<> ubound(arr) then oText.insertControlCharacter( tc, com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, False )
next
tc =otext.createtextcursorbyrange(otext)
sd= tc.createsortdescriptor
for i =0 to ubound(sd)
select case sd(i).name
case "IsSortInTable"
case "Delimiter"
if ismissing(delimeter) = false then sd(i).value =asc( delimeter)
case "IsSortColumns"
case "MaxSortFieldsCount"
case "SortFields"
t= sd(i).value
with t(0)
if ismissing(IsSortAscending0) = false then .IsAscending = IsSortAscending0
if ismissing(IsSortNumeric0) = false then .FieldType =com.sun.star.table.TableSortFieldType.NUMERIC
if ismissing(SortRowOrColumnNo0) = false then .field =SortRowOrColumnNo0
end with
with t(1)
if ismissing(IsSortAscending1) = false then .IsAscending = IsSortAscending1
if ismissing(IsSortNumeric1) = false then .FieldType =com.sun.star.table.TableSortFieldType.NUMERIC
if ismissing(SortRowOrColumnNo1) = false then .field =SortRowOrColumnNo1
end with
with t(2)
if ismissing(IsSortAscending2) = false then .IsAscending = IsSortAscending2
if ismissing(IsSortNumeric2) = false then .FieldType =com.sun.star.table.TableSortFieldType.NUMERIC
if ismissing(SortRowOrColumnNo2) = false then .field =SortRowOrColumnNo2
end with
sd(i).value=t
end select
next
tc.sort (sd)
i= 0
en= otext.createenumeration
do while en.hasmoreelements
p = en.nextelement
arr(i)=p.string
i = i+1
loop
doc.close false
sortarray2 = arr
end function
'char
'Delimiter contains the character that marks the separation of columns.
'boolean
'IsSortInTable determines if the content of a table is to be sorted.
'long
'SortRowOrColumnNo0 contains the row or column index used in the first search key.
'boolean
'IsSortNumeric0 determines if the sorting in the first search key is done numeric or alphanumeric order.
'boolean
'IsSortAscending0 determines if the sorting in the first search key is done in ascending or descending order.
'long
'SortRowOrColumnNo1 contains the row or column index used in the second search key.
'boolean
'IsSortNumeric1 determines if the sorting in the second search key is done in numeric or alphanumeric order.
'boolean
'IsSortAscending1 determines if the sorting in the second search key is done in ascending or descending order.
'long
'SortRowOrColumnNo2 contains the row or column index used in the third search key.
'boolean
'IsSortNumeric2 determines if the sorting in the third search key is done in numeric or alphanumeric order.
'boolean
'IsSortAscending2 determines if the sorting in the third search key is done in ascending or descending order.
'In
'