How to resolve the algorithm Sort using a custom comparator step by step in the OxygenBasic programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Sort using a custom comparator step by step in the OxygenBasic programming language

Table of Contents

Problem Statement

Sort an array (or list) of strings in order of descending length, and in ascending lexicographic order for strings of equal length. Use a sorting facility provided by the language/library, combined with your own callback comparison function.

Note:   Lexicographic order is case-insensitive.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Sort using a custom comparator step by step in the OxygenBasic programming language

Source code in the oxygenbasic programming language

uses generics 'containing sort macros
uses console
string sdata={"CC","Aa","aAa","bb","bbB","b","B","c","A"}
'
int count = countof sdata
'
macro filter(f,a)
=================
  'sdata[a]
  f=1 'allow all
end macro
'
macro compare(f,a,b)
====================
  int la=len sdata[a]
  int lb=len sdata[b]
  if la
    f=1 'descending length
  elseif la>lb
    '
  elseif ucase(sdata[a])>ucase(sdata[b])
    f=1 'ascending but case insensitive
  endif
end macro
'
NewSortIndex(index,count,rcount,filter,compare)
NewSortedData(sorted,sdata,index,rcount)
'
print "Count: " rcount cr cr
int i
for i=1 to rcount
  print sorted[i] cr
next
pause


  

You may also check:How to resolve the algorithm Sum and product puzzle step by step in the Wren programming language
You may also check:How to resolve the algorithm Create an HTML table step by step in the VBScript programming language
You may also check:How to resolve the algorithm Multifactorial step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Sorting algorithms/Insertion sort step by step in the Dart programming language
You may also check:How to resolve the algorithm Last Friday of each month step by step in the Elixir programming language