Tech Articles


Sorting a string array


I had a need to sort a string array and I found some code on Real Gagnon’s website that sorted numbers by creating a DataStore/DataWindow on the fly and used that to sort. Someone added a comment that for strings you need to define the column as char to avoid errors. I improved the code by adding a loop to determine the longest string in the array and using that when defining the DataWindow source.

 

public function boolean of_sortarray (ref string as_array[], string as_order);// —————————————————————————–

// SCRIPT:     of_SortArray

//

// PURPOSE:    This function sorts the passed array using a DataStore created

// on-the-fly.

//

// This is based on code from Real Gagnon:

// http://www.rgagnon.com/pbdetails/pb-0114.html

//

// ARGUMENTS:  as_array – Array of values to sort

// as_order – The sort order:

//

// Order value Resulting sort order

// ———————— —————————

// a, asc, ascending, ai, i Case-insensitive ascending

// d, desc, descending, di Case-insensitive descending

// as, s Case-sensitive ascending

// ds Case-sensitive descending

//

// DATE        PROG/ID DESCRIPTION OF CHANGE / REASON

// ———-  ——– —————————————————–

// 09/27/2016 RolandS Initial Coding

// —————————————————————————–

 

DataStore lds_sort

String ls_source, ls_error

Long ll_idx, ll_max, ll_len, ll_maxlen

Integer li_rc

 

// determine maximum length of values

ll_max = UpperBound(as_array)

For ll_idx = 1 To ll_max

  ll_len = Len(as_array[ll_idx])

  If ll_len > ll_maxlen Then

  ll_maxlen = ll_len

  End If

Next

 

// build DataWindow source code

ls_source = “release 6; datawindow( processing=0 ) ” + &

  “table(column=(type=char(” + String(ll_maxlen) + &

  “) name=array dbname=~”array~” ) )”

 

// create DataStore

lds_sort = Create DataStore

li_rc = lds_sort.Create(ls_source, ls_error)

If li_rc = 1 Then

  // put data into DataStore

  lds_sort.Object.array.Current = as_array

  // sort the data

  lds_sort.SetSort(“array ” + as_order)

  lds_sort.Sort()

  // set array to sorted data

  as_array = lds_sort.Object.array.Current

Else

  Return False

End If

 

// destroy DataStore

Destroy lds_sort

 

Return True

 

end function

Comments (2)
Wednesday, Jun 12 2019

ls_source = “release 6; datawindow( processing=0 ) ” + &

Instead of hardcodidng release number, you can create a function uf_get_pb_version() with this code:

/**********************************************************************************************************************
Dscr: returns the major version number of PowerBuilder, for example: "9" (not "9.0"!)
***********************************************************************************************************************
Ret: PB version as string
**********************************************************************************************************************/
int li_rc
string ls_pb_version
environment lenv

li_rc = GetEnvironment(ref lenv)
if li_rc 1 then
MessageBox(this.ClassName() + ".uf_get_pb_version", "GetEnvironment() failed.", StopSign!)
return ""
end if

ls_pb_version = String(lenv.pbmajorrevision)

return ls_pb_version

Comment was last edited 5 years ago by Michael Zuskin
0

Wednesday, Jun 12 2019

The release version of a DataWindow object does not have to match the version of PowerBuilder you are using, it only has to be lower. You could make it match with GetEnvironment but it is not necessary.

0

Find Articles by Tag

OrcaScript TFS Graph DragDrop API Source Code Transaction Database Table Design Branch & Merge Debugging Validation Authentication Oracle Messagging License C# Application Encryption OLE TLS/SSL REST PDFlib TortoiseGit Web API Variable SqlExecutor JSONParser Mobile UI Modernization Database OAuth Repository JSON Event Handler 32-bit PowerBuilder (Appeon) JSONGenerator InfoMaker Icon Testing Elevate Conference Trial PowerServer Web File External Functions PBDOM Outlook PowerScript (PS) Database Table Data Debug Windows 10 Icons Encoding Resize Performance WinAPI BLOB Export JSON Git PowerServer Mobile SQL RibbonBar Source Control Event Handling NativePDF PostgreSQL PowerBuilder PDF Linux OS Configuration SnapDevelop SVN .NET Assembly Debugger DataType Android Deployment Database Table Schema GhostScript MessageBox Database Profile Window Windows OS CoderObject Script UI Themes Bug ODBC Event XML DataWindow SOAP SQL Server .NET Std Framework DevOps Interface SqlModelMapper Platform iOS Error Class CrypterObject 64-bit Database Painter WebBrowser Jenkins Model ActiveX Import RESTClient Export Migration DataWindow JSON SDK Sort Expression Excel SnapObjects Menu UI CI/CD Authorization PostgreSQL ODBC driver Automated Testing Syntax PowerBuilder Compiler COM Filter IDE TreeView DLL OAuth 2.0 Database Connection Installation Charts Web Service Proxy Database Object .NET DataStore Data Stored Procedure Azure RichTextEdit Control Open Source RibbonBar Builder HTTPClient Import JSON Array Text PFC Service