How to resolve the algorithm Sort using a custom comparator step by step in the FutureBasic 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 FutureBasic 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 FutureBasic programming language

Source code in the futurebasic programming language

include "NSLog.incl"

local fn CustomComparator( obj1 as CFTypeRef, obj2 as CFTypeRef, context as ptr ) as NSComparisonResult
  NSComparisonResult result = fn StringCaseInsensitiveCompare( obj1, obj2 )
end fn = result

local fn ComparatorStringSort( wordString as CFStringRef ) as CFStringRef
  CFArrayRef   stringArray = fn StringComponentsSeparatedByString( wordString, @" " )
  CFArrayRef   sortedArray = fn ArraySortedArrayUsingFunction( stringArray, @fn CustomComparator, NULL )
  CFStringRef    sortedStr = fn ArrayComponentsJoinedByString( sortedArray, @"\n" )
end fn = sortedStr

NSLog( @"%@", fn ComparatorStringSort( @"The quick brown fox jumped over the lazy dog's back" ) )

HandleEvents

  

You may also check:How to resolve the algorithm Jensen's Device step by step in the ARM Assembly programming language
You may also check:How to resolve the algorithm Sleep step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm Comments step by step in the MBS programming language
You may also check:How to resolve the algorithm Multiplication tables step by step in the J programming language
You may also check:How to resolve the algorithm Empty program step by step in the Hare programming language