GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Download
  • News
  • Community
  • Resources
  • Language Reference
  • SmallBASIC Manual

This view

  • Original page
  • New address

GeezerVison is showing smallbasic.github.io — SmallBASIC | SORT

Edit History

SORT

SORT byref A [USE cmpfunc( x, y )]

Home > Reference > Data

Sorts an array A in ascending order. The sorted array is return as A, therefore overwriting the initial array. If a compare function cmpfunc is specified, this function will be used for comparision. The compare function takes two elements of A as x, y to compare and must return:

  • -1 if x is to be placed before y
  • 1 if y is to be placed before x
  • 0 if it doesn’t matter which is placed first (which is usually the case when the elements are equal)

Example 1: Sorting in ascending order

A = [5, 3, 8, 2, 1, 7, 9]
sort A
print A                         ' Output [1,2,3,5,7,8,9]

Example 2: Sorting in ascending order using a compare function

func cmpfunc_ascending(x, y)
    if x == y
        return 0
    elseif x > y
        return 1
    else
        return -1
    endif
end

A = [5, 3, 8, 2, 1, 7, 9]
sort A use cmpfunc_ascending(x, y)
print A                         ' Output [1,2,3,5,7,8,9]

Example 3: Sorting in descending order using a compare function

func cmpfunc_descending(x, y)
    if x == y
        return 0
    elseif x < y
        return 1
    else
        return -1
    endif
end

A = [5, 3, 8, 2, 1, 7, 9]
sort A use cmpfunc_descending(x, y)
print A                         ' Output [9,8,7,5,3,2,1]
Code samples using SORT
003 conditional branching.bas   004 loops.bas   006 arrays+.bas   agendus.bas   betrayal: crows ii.bas   crgb.bas   filemanager.bas   getfilename.bas   img2ascii.bas   memory match game.bas   nim.bas   realpolitik.bas   save as.bas   sokoban.bas   sokoban.bas   sokoban.bas   two tables game.bas  
Data
APPEND   ARRAY   DATA   DELETE   DIM   EMPTY   ERASE   INSERT   ISARRAY   ISDIR   ISFILE   ISLINK   ISMAP   ISNUMBER   ISSTRING   LBOUND   LEN   READ   REDIM   RESTORE   SEARCH   SORT   SWAP   UBOUND  
This page was last edited on Thu, 10 Aug 2023 16:57:02 +0200 | Markdown processed with pandoc 3.1.12.1
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.