Order two numerical lists: Difference between revisions

m
m (ordered the categories to put the sorting algorithms in the proper place.)
 
(18 intermediate revisions by 14 users not shown)
Line 17:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">[1,2,1,3,2] < [1,2,0,4,4,0,0,0]</langsyntaxhighlight>
 
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<syntaxhighlight lang="aarch64 assembly">
<lang AArch64 Assembly>
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program orderlist64.s */
Line 172:
.include "../includeARM64.inc"
 
</syntaxhighlight>
</lang>
 
=={{header|ACL2}}==
Line 182:
ACL2 !>(lexorder '(1 2 4) '(1 2 3))
NIL</pre>
 
=={{header|Action!}}==
<syntaxhighlight lang="action!">INT FUNC Compare(INT ARRAY x INT xLen INT ARRAY y INT yLen)
INT i,len
 
len=xLen
IF len>yLen THEN
len=yLen
FI
FOR i=0 TO len-1
DO
IF x(i)>y(i) THEN
RETURN (1)
ELSEIF x(i)<y(i) THEN
RETURN (-1)
FI
OD
IF xLen>yLen THEN
RETURN (1)
ELSEIF xLen<yLen THEN
RETURN (-1)
FI
RETURN (0)
 
BYTE FUNC Less(INT ARRAY x INT xLen INT ARRAY y INT yLen)
IF Compare(x,xLen,y,yLen)<0 THEN
RETURN (1)
FI
RETURN (0)
 
PROC PrintArray(INT ARRAY x INT len)
INT i
 
Put('[)
FOR i=0 TO len-1
DO
PrintI(x(i))
IF i<len-1 THEN
Put(' )
FI
OD
Put('])
RETURN
 
PROC Test(INT ARRAY x INT xLen INT ARRAY y INT yLen)
PrintArray(x,xLen)
IF Less(x,xLen,y,yLen) THEN
Print(" < ")
ELSE
Print(" >= ")
FI
PrintArray(y,yLen) PutE()
RETURN
 
PROC Main()
INT ARRAY a=[1 2 1 5 2]
INT ARRAY b=[1 2 1 5 2 2]
INT ARRAY c=[1 2 3 4 5]
INT ARRAY d=[1 2 3 4 5]
Test(a,5,b,6)
Test(b,6,a,5)
Test(b,6,c,5)
Test(c,5,b,6)
Test(c,5,d,5)
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Order_two_numerical_lists.png Screenshot from Atari 8-bit computer]
<pre>
[1 2 1 5 2] < [1 2 1 5 2 2]
[1 2 1 5 2 2] >= [1 2 1 5 2]
[1 2 1 5 2 2] < [1 2 3 4 5]
[1 2 3 4 5] >= [1 2 1 5 2 2]
[1 2 3 4 5] >= [1 2 3 4 5]
</pre>
 
=={{header|Ada}}==
Line 187 ⟶ 262:
This also includes arrays of user defined types, using the type definition order from smallest to largest.
Demonstrated in the program below:
<syntaxhighlight lang="ada">
<lang Ada>
with Ada.Text_IO; use Ada.Text_IO;
procedure Order is
Line 210 ⟶ 285:
Put_Line (Boolean'Image (List6 > List7)); -- True
end Order;
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 218 ⟶ 293:
TRUE
TRUE
</pre>
 
=={{header|ALGOL 68}}==
<syntaxhighlight lang="algol68">BEGIN # compare lists (rows) of integers #
# returns TRUE if there is an element in a that is < the corresponding #
# element in b and all previous elements are equal; FALSE otherwise #
OP < = ( []INT a, b )BOOL:
IF INT a pos := LWB a;
INT b pos := LWB b;
BOOL equal := TRUE;
WHILE a pos <= UPB a AND b pos <= UPB b AND equal DO
equal := a[ a pos ] = b[ b pos ];
IF equal THEN
a pos +:= 1;
b pos +:= 1
FI
OD;
NOT equal
THEN
# there is an element in a and b that is not equal #
a[ a pos ] < b[ b pos ]
ELSE
# all elements are equal or one list is shorter #
# a is < b if a has fewer elements #
a pos > UPB a AND b pos <= UPB b
FI # < # ;
 
# tests a < b has the expected result #
PROC test = ( STRING a name, []INT a, STRING b name, []INT b, BOOL expected )VOID:
BEGIN
BOOL result = a < b;
print( ( a name, IF result THEN " < " ELSE " >= " FI, b name
, IF result = expected THEN "" ELSE ", NOT as expected" FI
, newline
)
)
END # test # ;
 
# test cases as in the BBC basic sample #
[]INT list1 = ( 1, 2, 1, 5, 2 );
[]INT list2 = ( 1, 2, 1, 5, 2, 2 );
[]INT list3 = ( 1, 2, 3, 4, 5 );
[]INT list4 = ( 1, 2, 3, 4, 5 );
test( "list1", list1, "list2", list2, TRUE );
test( "list2", list2, "list3", list3, TRUE );
test( "list3", list3, "list4", list4, FALSE );
 
# additional test cases #
[]INT list5 = ( 9, 0, 2, 1, 0 );
[]INT list6 = ( 4, 0, 7, 7 );
[]INT list7 = ( 4, 0, 7 );
[]INT list8 = ( );
test( "list5", list5, "list6", list6, FALSE );
test( "list6", list6, "list7", list7, FALSE );
test( "list7", list7, "list8", list8, FALSE );
test( "list8", list8, "list7", list7, TRUE )
 
END</syntaxhighlight>
{{out}}
<pre>
list1 < list2
list2 < list3
list3 >= list4
list5 >= list6
list6 >= list7
list7 >= list8
list8 < list7
</pre>
 
=={{header|ALGOL W}}==
{{Trans|ALGOL 68}}
<syntaxhighlight lang="algolw">begin % compare lists (rows) of integers %
% returns TRUE if there is an element in a that is < the corresponding %
% element in b and all previous elements are equal, FALSE otherwise %
% the bounds of a and b should aLb :: aUb and bLb :: bUb %
logical procedure iLT ( integer array a ( * )
; integer value aLb, aUb
; integer array b ( * )
; integer value bLb, bUb
) ;
begin
integer aPos, bPos;
logical equal;
aPos := aLb;
bPos := bLb;
equal := true;
while aPos <= aUb and bPos <= bUb and equal do begin
equal := a( aPos ) = b( bPos );
if equal then begin
aPos := aPOs + 1;
bPos := bPos + 1
end if_equal
end while_more_elements_and_equal ;
if not equal
then % there is an element in a and b that is not equal %
a( aPos ) < b( bPos )
else % all elements are equal or one list is shorter %
% a is < b if a has fewer elements %
aPos > aUb and bPos <= bUb
end iLT ;
% tests a < b has the expected result %
procedure test ( string(5) value aName
; integer array a ( * )
; integer value aLb, aUb
; string(5) value bName
; integer array b ( * )
; integer value bLb, bUb
; logical value expected
) ;
begin
logical isLt;
isLt := iLT( a, aLb, aUb, b, bLb, bUb );
write( aName, if isLt then " < " else " >= ", bName
, if isLt = expected then "" else ", NOT as expected"
)
end test ;
 
integer array list1, list3, list4 ( 1 :: 5 );
integer array list2 ( 1 :: 6 );
integer array list5 ( 1 :: 5 );
integer array list6 ( 1 :: 4 );
integer array list7 ( 1 :: 3 );
integer array list8 ( 1 :: 1 );
integer aPos;
% test cases as in the BBC basic sample %
aPos := 1; for i := 1, 2, 1, 5, 2 do begin list1( aPos ) := i; aPos := aPos + 1 end;
aPos := 1; for i := 1, 2, 1, 5, 2, 2 do begin list2( aPos ) := i; aPos := aPos + 1 end;
aPos := 1; for i := 1, 2, 3, 4, 5 do begin list3( aPos ) := i; aPos := aPos + 1 end;
aPos := 1; for i := 1, 2, 3, 4, 5 do begin list4( aPos ) := i; aPos := aPos + 1 end;
test( "list1", list1, 1, 5, "list2", list2, 1, 6, true );
test( "list2", list2, 1, 6, "list3", list3, 1, 5, true );
test( "list3", list3, 1, 5, "list4", list4, 1, 5, false );
% additional test cases %
aPos := 1; for i := 9, 0, 2, 1, 0 do begin list5( aPos ) := i; aPos := aPos + 1 end;
aPos := 1; for i := 4, 0, 7, 7 do begin list6( aPos ) := i; aPos := aPos + 1 end;
aPos := 1; for i := 4, 0, 7 do begin list7( aPos ) := i; aPos := aPos + 1 end;
test( "list5", list5, 1, 5, "list6", list6, 1, 4, false );
test( "list6", list6, 1, 4, "list7", list7, 1, 3, false );
test( "list7", list7, 1, 3, "list8", list8, 1, 0, false );
test( "list8", list8, 1, 0, "list7", list7, 1, 3, true )
end.</syntaxhighlight>
{{out}}
<pre>
list1 < list2
list2 < list3
list3 >= list4
list5 >= list6
list6 >= list7
list7 >= list8
list8 < list7
</pre>
 
=={{header|Aime}}==
<langsyntaxhighlight lang="aime">ordl(list a, b)
{
integer i, l, o;
Line 248 ⟶ 473:
 
0;
}</langsyntaxhighlight>
 
=={{header|AppleScript}}==
Line 255 ⟶ 480:
 
<= is not defined over lists in AppleScript
<langsyntaxhighlight AppleScriptlang="applescript">-- <= for lists
-- compare :: [a] -> [a] -> Bool
on compare(xs, ys)
Line 298 ⟶ 523:
missing value
end if
end uncons</langsyntaxhighlight>
 
{{Out}}
<syntaxhighlight lang AppleScript="applescript">{false, true}</langsyntaxhighlight>
 
----
Here's an iterative implementation:
 
<langsyntaxhighlight lang="applescript">on listAComesBeforeListB(a, b)
set aLength to (count a)
set bLength to (count b)
Line 337 ⟶ 562:
end repeat
 
return {a:a, b:b, | a < b |: listAComesBeforeListB(a, b)},</langsyntaxhighlight>
 
{{Out}}
Examples:
<langsyntaxhighlight lang="applescript">{a:{3, 5, 6, 5, 2, 1, 4}, b:{5, 8, 9, 2, 0, 1, 9, 0, 4, 10}, | a < b |:true}
{a:{10, 9, 5, 8, 8, 4, 3}, b:{5, 10, 8, 9, 10, 8, 7, 8}, | a < b |:false}
{a:{0, 10}, b:{0, 10}, | a < b |:false}
{a:{0, 10}, b:{0, 10, 5}, | a < b |:true}</langsyntaxhighlight>
 
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang="arm assembly">
<lang ARM Assembly>
/* ARM assembly Raspberry PI */
/* program orderlist.s */
Line 503 ⟶ 728:
pop {r0,r1,r2,r7,lr} @ restaur registers */
bx lr @ return
</syntaxhighlight>
</lang>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="rebol">compareLists: function [a,b][
loop 0..min @[size a, size b] 'i [
if a\[i] < b\[i] -> return true
if a\[i] > b\[i] -> return false
]
return less? size a size b
]
 
alias.infix '<=> 'compareLists
 
do [
print [1 2 1 3 2] <=> [1 2 0 4 4 0 0 0]
]</syntaxhighlight>
 
{{out}}
 
<pre>false</pre>
 
=={{header|AutoHotkey}}==
{{works with|AutoHotkey_L}}
The function is overkill as we can just compare the list's ObjMaxIndex()
<langsyntaxhighlight AHKlang="ahk">List1 := [1,2,1,3,2]
List2 := [1,2,0,4,4,0,0,0]
MsgBox % order(List1, List2)
Line 514 ⟶ 759:
order(L1, L2){
return L1.MaxIndex() < L2.MaxIndex()
}</langsyntaxhighlight>
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f ORDER_TWO_NUMERICAL_LISTS.AWK
BEGIN {
Line 542 ⟶ 787:
return(ans)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 549 ⟶ 794:
list3>=list4
</pre>
 
 
=={{header|BASIC256}}==
{{trans|FreeBASIC}}
<syntaxhighlight lang="basic256">
arraybase 1
dim list1(5): dim list2(6): dim list3(5): dim list4(5)
list1 = {1, 2, 1, 5, 2}
list2 = {1, 2, 1, 5, 2, 2}
list3 = {1, 2, 3, 4, 5}
list4 = {1, 2, 3, 4, 5}
 
if Orden(list1[], list2[]) then print "list1 < list2" else print "list 1>= list2"
if Orden(list2[], list3[]) then print "list2 < list3" else print "list2 >= list3"
if Orden(list3[], list4[]) then print "list3 < list4" else print "list3 >= list4"
end
 
function Orden(listA[], listB[])
i = 0
l1 = listA[?]
l2 = listB[?]
while (listA[i] = listB[i]) and i < l1 and i < l2
i = i + 1
end while
if listA[?] < listB[?] then return True
if listA[?] > listB[?] then return False
return l1 < l2
end function
</syntaxhighlight>
{{out}}
<pre>
Igual que la entrada de BBC BASIC.
</pre>
 
 
=={{header|BBC BASIC}}==
'Ordered before' means 'less than' (see [[Talk:Order_two_numerical_lists|talk page]]).
<langsyntaxhighlight lang="bbcbasic"> DIM list1(4) : list1() = 1, 2, 1, 5, 2
DIM list2(5) : list2() = 1, 2, 1, 5, 2, 2
DIM list3(4) : list3() = 1, 2, 3, 4, 5
Line 570 ⟶ 849:
IF list1(i%) < list2(i%) THEN = TRUE
IF list1(i%) > list2(i%) THEN = FALSE
= l1% < l2%</langsyntaxhighlight>
{{out}}
<pre>
Line 585 ⟶ 864:
In that case the function outputs FALSE.
Notice that if the arguments are the same, evaluation of the sum produces the product of one of the terms and a factor two. This complicates the pattern a bit.
<langsyntaxhighlight lang="bracmat">( 1 2 3 4 5:?List1
& 1 2 1 5 2 2:?List2
& 1 2 1 5 2:?List3
Line 608 ⟶ 887:
& gt$(!List5,!List6)
& gt$(!List6,!List7)
);</langsyntaxhighlight>
{{out}}
<pre>TRUE
Line 618 ⟶ 897:
 
=={{header|C}}==
<langsyntaxhighlight lang="c">int list_cmp(int *a, int la, int *b, int lb)
{
int i, l = la;
Line 628 ⟶ 907:
if (la == lb) return 0;
return la > lb ? 1 : -1;
}</langsyntaxhighlight>
This funciton returns one of three states, not a boolean. One can define boolean comparisons, such as <code>list_less_or_eq</code>, based on it:<langsyntaxhighlight lang="c">#define list_less_or_eq(a,b,c,d) (list_cmp(a,b,c,d) != 1)</langsyntaxhighlight>
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang="csharp">namespace RosettaCode.OrderTwoNumericalLists
{
using System;
Line 674 ⟶ 953:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>False</pre>
Line 680 ⟶ 959:
=={{header|C++}}==
The built-in comparison operators already do this:
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <vector>
 
Line 702 ⟶ 981:
std::cout << std::boolalpha << (a < b) << std::endl; // prints "false"
return 0;
}</langsyntaxhighlight>
 
=={{header|clojure}}==
 
<langsyntaxhighlight lang="clojure">
(defn lex? [a b]
(compare a b))
</syntaxhighlight>
</lang>
 
=={{header|Common Lisp}}==
<langsyntaxhighlight Lisplang="lisp">(defun list< (a b)
(cond ((not b) nil)
((not a) t)
((= (first a) (first b))
(list< (rest a) (rest b)))
(t (< (first a) (first b)))))</langsyntaxhighlight>
 
Alternate version
 
<langsyntaxhighlight Lisplang="lisp">(defun list< (a b)
(let ((x (find-if-not #'zerop (mapcar #'- a b))))
(if x (minusp x) (< (length a) (length b)))))</langsyntaxhighlight>
 
=={{header|D}}==
The built-in comparison operators already do this:
<langsyntaxhighlight lang="d">void main() {
assert([1,2,1,3,2] >= [1,2,0,4,4,0,0,0]);
}</langsyntaxhighlight>
=={{header|Delphi}}==
{{libheader| System.SysUtils}}
Line 735 ⟶ 1,014:
{{Trans|Java}}
This is not a full translate of [https://rosettacode.org/wiki/Order_two_numerical_lists#Java Java], just adaptation and aplication of method previus introduced.
<syntaxhighlight lang="delphi">
<lang Delphi>
program Order_two_numerical_lists;
 
Line 770 ⟶ 1,049:
'not', 'a', 'test']));
readln;
end.</langsyntaxhighlight>
{{out}}
<pre>TRUE
Line 778 ⟶ 1,057:
FALSE
TRUE</pre>
 
=={{header|EasyLang}}==
{{trans|C}}
<syntaxhighlight>
func listcmp a[] b[] .
for i to lower len a[] len b[]
if a[i] < b[i]
return -1
elif a[i] > b[i]
return 1
.
.
if len a[] < len b[]
return -1
elif len a[] > len b[]
return 1
.
return 0
.
print listcmp [ 2 4 5 ] [ 2 3 1 ]
print listcmp [ 2 3 1 ] [ 2 3 1 ]
print listcmp [ 2 3 1 ] [ 2 3 1 3 ]
</syntaxhighlight>
 
=={{header|Ela}}==
 
<langsyntaxhighlight lang="ela">[] <. _ = true
_ <. [] = false
(x::xs) <. (y::ys) | x == y = xs <. ys
| else = x < y
 
[1,2,1,3,2] <. [1,2,0,4,4,0,0,0]</langsyntaxhighlight>
 
 
=={{header|Elixir}}==
The built-in comparison functions already do this (not only for lists of numbers, but for any arbitrary data type).
<langsyntaxhighlight lang="elixir">iex(1)> [1,2,3] < [1,2,3,4]
true
iex(2)> [1,2,3] < [1,2,4]
true</langsyntaxhighlight>
 
=={{header|Erlang}}==
Builtin. Example use from Erlang shell:
<syntaxhighlight lang="erlang">
<lang Erlang>
5> [1,2,3] < [1,2,3,4].
true
6> [1,2,3] < [1,2,4].
true
</syntaxhighlight>
</lang>
 
=={{header|F_Sharp|F#}}==
By using the Collection.Seq Module the static method Seq.compareWith fits our needs.
<langsyntaxhighlight lang="fsharp">let inline cmp x y = if x < y then -1 else if x = y then 0 else 1
let before (s1 : seq<'a>) (s2 : seq<'a>) = (Seq.compareWith cmp s1 s2) < 0
 
Line 824 ⟶ 1,125:
([0; 0], [1]);
]
|> List.iter (fun (x, y) -> printf "%A %s %A\n" x (if before x y then "< " else ">=") y)</langsyntaxhighlight>
{{out}}
<pre>[0] >= []
Line 848 ⟶ 1,149:
=={{header|FreeBASIC}}==
{{trans|BBC BASIC}}
<langsyntaxhighlight lang="freebasic">Dim Shared list1(4) As Integer = {1, 2, 1, 5, 2}
Dim Shared list2(5) As Integer = {1, 2, 1, 5, 2, 2}
Dim Shared list3(4) As Integer = {1, 2, 3, 4, 5}
Line 869 ⟶ 1,170:
If Orden(list3(), list4()) Then Print "list3<list4" Else Print "list3>=list4"
 
Sleep</langsyntaxhighlight>
{{out}}
<pre>
Line 877 ⟶ 1,178:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 949 ⟶ 1,250:
}
return b
}</langsyntaxhighlight>
{{out}}
<pre>
Line 981 ⟶ 1,282:
=={{header|Groovy}}==
Solution:
<langsyntaxhighlight lang="groovy">class CList extends ArrayList implements Comparable {
CList() { }
CList(Collection c) { super(c) }
Line 990 ⟶ 1,291:
comp ? comp[0] <=> comp[1] : this.size() <=> that.size()
}
}</langsyntaxhighlight>
 
Test:
<langsyntaxhighlight lang="groovy">CList a, b; (a, b) = [[], []]; assert ! (a < b)
b = [1] as CList; assert (a < b)
a = [1] as CList; assert ! (a < b)
Line 1,001 ⟶ 1,302:
b = [2, -1, 0] as CList; assert ! (a < b)
b = [2, -1, 0, -17] as CList; assert (a < b)
a = [2, 8, 0] as CList; assert ! (a < b)</langsyntaxhighlight>
 
=={{header|Haskell}}==
The built-in comparison operators already do this:
<langsyntaxhighlight lang="haskell">Prelude> [1,2,1,3,2] < [1,2,0,4,4,0,0,0]
False</langsyntaxhighlight>
 
=={{header|Icon}} and {{header|Unicon}}==
List_llt is written in the style of all Icon/Unicon relational operators returning its right argument if successful and signaling failure otherwise.
 
<langsyntaxhighlight Iconlang="icon">procedure main()
write( if list_llt([1,2,1,3,2],[1,2,0,4,4,0,0,0]) then "true" else "false" )
end
Line 1,021 ⟶ 1,322:
else if L1[i] >> L2[i] then fail
if *L1 < *L2 then return L2
end</langsyntaxhighlight>
 
=={{header|J}}==
Line 1,030 ⟶ 1,331:
However numeric scalars sort ahead of vectors, i.e. are different from length one lists.
 
<langsyntaxhighlight lang="j">before=: -.@(-: /:~)@,&<~</langsyntaxhighlight>
 
<langsyntaxhighlight lang="j">cmp=: {.@\:@,:&(,&__)</langsyntaxhighlight>
 
Below demonstrates non-decreasing order cmp treats length one vector same as scalar
 
<syntaxhighlight lang="j">
<lang j>
cmp&.>"{~ ('';0;(,0);1;(,1);1 1)
┌─┬─┬─┬─┬─┬─┐
Line 1,065 ⟶ 1,366:
├─┼─┼─┼─┼─┼─┤
│0│0│0│0│0│0│
└─┴─┴─┴─┴─┴─┘</langsyntaxhighlight>
 
=={{header|Java}}==
Line 1,071 ⟶ 1,372:
{{trans|Common Lisp}}
There are a few methods here. The method named "ordered" which works on arrays is a translation of [[#Common Lisp|Common Lisp]]. The other two are loose translations of [[#Tcl|Tcl]] (some tweaks were needed to get the length checks to work out) and are probably better options.
<langsyntaxhighlight lang="java5">import java.util.Arrays;
import java.util.List;
 
Line 1,104 ⟶ 1,405:
return i == first.length;
}
}</langsyntaxhighlight>
 
=={{header|JavaScript}}==
Line 1,112 ⟶ 1,413:
<= is already defined for numeric lists in JavaScript
 
<langsyntaxhighlight JavaScriptlang="javascript">(() => {
'use strict';
 
Line 1,129 ⟶ 1,430:
// --> [false, true]
})()
</syntaxhighlight>
</lang>
 
{{Out}}
<syntaxhighlight lang JavaScript="javascript">[false, true]</langsyntaxhighlight>
 
=={{header|Joy}}==
<syntaxhighlight lang="joy">
<lang Joy>
DEFINE order ==
[equal] [false]
[[[[size] dip size <=] [[<=] mapr2 true [and] fold]] [i] map i and]
ifte.
</syntaxhighlight>
</lang>
 
Using it:
Line 1,153 ⟶ 1,454:
 
=={{header|jq}}==
jq's builtin comparison operators use lexicographic ordering for arrays in general, not just arrays of integers.<langsyntaxhighlight lang="jq">
[1,2,3] < [1,2,3,4] # => true
[1,2,3] < [1,2,4] # => true
[1,2,3] < [1,2,3] # => false</langsyntaxhighlight>
 
=={{header|Julia}}==
{{works with|Julia|0.6}}
 
<langsyntaxhighlight lang="julia">function islexless(a::AbstractArray{<:Real}, b::AbstractArray{<:Real})
for (x, y) in zip(a, b)
if x == y continue end
Line 1,173 ⟶ 1,474:
println("List not sorted:\n - ", join(tests, "\n - "))
sort!(tests; lt=islexless)
println("List sorted:\n - ", join(tests, "\n - "))</langsyntaxhighlight>
 
{{out}}
Line 1,194 ⟶ 1,495:
 
=={{header|Klingphix}}==
<langsyntaxhighlight Klingphixlang="klingphix">include ..\Utilitys.tlhy
 
( 1 2 3 ) ( 1 2 3 4 ) less ?
Line 1,202 ⟶ 1,503:
( 1 2 3 ) ( 1 2 4 ) less ?
 
"End " input</langsyntaxhighlight>
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.0.6
 
operator fun <T> List<T>.compareTo(other: List<T>): Int
Line 1,232 ⟶ 1,533:
println()
for (i in 0 until lists.size - 1) println("list${i + 1} > list${i + 2} = ${lists[i] > lists[i + 1]}")
}</langsyntaxhighlight>
 
{{out}}
Line 1,258 ⟶ 1,559:
=={{header|Lasso}}==
This is built into the Lasso comparison operators
<langsyntaxhighlight Lassolang="lasso">local(
first = array(1,2,1,3,2),
second = array(1,2,0,4,4,0,0,0),
Line 1,268 ⟶ 1,569:
second = array(1,2,0,4,4,0,0,0),
)
#first < #second</langsyntaxhighlight>
{{out}}
<pre>false
Line 1,276 ⟶ 1,577:
Uses standard '=' notation
 
<langsyntaxhighlight lang="logo">print [1 2] = [1 2]
print [1 2] = [1 2 3]
print [1 3] = [1 2]
Line 1,283 ⟶ 1,584:
make "list1 [1 2 3 4 5 6]
make "list2 [1 2 3 4 5 7]
print :list1 = :list2</langsyntaxhighlight>
 
{{out}}
<langsyntaxhighlight lang="logo">true
false
false
false
false</langsyntaxhighlight>
 
=={{header|Lua}}==
Line 1,296 ⟶ 1,597:
In Lua tables with numerical indices are used as lists or arrays and they do not support comparison out-of-the-box, so a function is needed to implement the comparison:
<langsyntaxhighlight lang="lua">function arraycompare(a, b)
for i = 1, #a do
if b[i] == nil then
Line 1,306 ⟶ 1,607:
end
return true
end</langsyntaxhighlight>
 
Here is some demonstration code:
 
<langsyntaxhighlight lang="lua">function randomarray()
local t = {}
for i = 1, math.random(1, 10) do
Line 1,329 ⟶ 1,630:
arraycompare(a, b) and "<=" or ">",
table.concat(b, ', ')))
end</langsyntaxhighlight>
 
{{out}} (time used as random seed: 1413127434):
Line 1,346 ⟶ 1,647:
 
=={{header|Maple}}==
<langsyntaxhighlight Maplelang="maple">orderLists := proc(num1,num2)
local len1, len2,i:
len1,len2 := numelems(num1),numelems(num2):
Line 1,355 ⟶ 1,656:
end do:
return evalb(len1 < len2):
end proc:</langsyntaxhighlight>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">order[List1_, List2_] := With[{
<lang Mathematica>
order[List1_, List2_] := With[{
L1 = List1[[1 ;; Min @@ Length /@ {List1, List2}]],
L2 = List2[[1 ;; Min @@ Length /@ {List1, List2}]]
Line 1,366 ⟶ 1,666:
Length[List1] < Length[List2],
Thread[Order[L1, L2]] == 1
]]</langsyntaxhighlight>
 
<pre>Example use:
order[ {1, 2, 1, 3, 2}, {1, 2, 0, 4, 4, 0, 0, 0} ]
->False
 
order[ {1, 2}, {1, 2, 4, 4, 0, 0} ]
->True</pre>
 
=={{header|Maxima}}==
<langsyntaxhighlight lang="maxima">"<<"(a,b):=block([n:min(length(a),length(b))],
catch(for i thru n do (if a[i]#b[i] then throw(is(a[i]<b[i]))),
throw(is(length(a)<length(b)))))$
Line 1,388 ⟶ 1,687:
 
[1,2] << [1,2];
false</langsyntaxhighlight>
 
=={{header|Mercury}}==
Line 1,394 ⟶ 1,693:
For a particular numerical type, you can get away with
 
<langsyntaxhighlight Mercurylang="mercury">:- pred lt(list(int)::in, list(int)::in) is semidet.
lt([], [_|_]).
lt([H1|T1], [H2|T2]) :- H1 =< H2, T1 `lt` T2.</langsyntaxhighlight>
 
For a list of any numerical type, one way would be to use a typeclass:
 
<langsyntaxhighlight Mercurylang="mercury">:- pred lt(list(T)::in, list(T)::in) is semidet <= comparable(T).
lt([], [_|_]).
lt([H1|T1], [H2|T2]) :- H1 =< H2, T1 `lt` T2.</langsyntaxhighlight>
 
... which you would have to create:
 
<langsyntaxhighlight Mercurylang="mercury">:- module comparable.
:- interface.
:- import_module int, float, integer, list.
Line 1,432 ⟶ 1,731:
 
% pred lt
% pred lte</langsyntaxhighlight>
 
Which would be used in this way - note the typeclass and the comparison operator.
 
<langsyntaxhighlight Mercurylang="mercury">:- pred test(list(T), list(T), io, io) <= comparable(T).
:- mode test(in, in, di, uo) is det.
test(A, B) -->
io.write(A), io.write_string(" < "), io.write(B),
io.write_string(" : "), io.write_string(S), io.nl,
{ A < B -> S = "yes" ; S = "no" }.</langsyntaxhighlight>
 
=={{header|Miranda}}==
<syntaxhighlight lang="miranda">main :: [sys_message]
main = [Stdout (lay (map show [
order [1,2,1,3,2] [1,2,0,4,4,0,0,0],
order [1,2,0,4,4,0,0,0] [1,2,1,3,2]]))]
 
order :: [*]->[*]->bool
order as [] = True
order [] as = False
order (a:as) (b:bs) = a < b, if a ~= b
= order as bs, otherwise</syntaxhighlight>
{{out}}
<pre>False
True</pre>
 
=={{header|Nim}}==
<langsyntaxhighlight lang="nim">proc `<`[T](a, b: openarray[T]): bool =
for i in 0 .. min(a.len, b.len):
if a[i] < b[i]: return true
Line 1,450 ⟶ 1,764:
return a.len < b.len
 
echo([1,2,1,3,2] < [1,2,0,4,4,0,0,0])</langsyntaxhighlight>
{{out}}
<pre>false</pre>
Line 1,457 ⟶ 1,771:
 
The built-in comparison operators already do this for lists (although this is not documented):
<langsyntaxhighlight lang="ocaml"># [1;2;1;3;2] < [1;2;0;4;4;0;0;0];;
- : bool = false</langsyntaxhighlight>
 
(Warning: However, the built-in comparison operators do not do this for arrays:
<langsyntaxhighlight lang="ocaml"># [|1;2;1;3;2|] < [|1;2;0;4;4;0;0;0|];;
- : bool = true</langsyntaxhighlight>
)
 
But we could write it explicitly this way:
 
<langsyntaxhighlight lang="ocaml">let rec ordered_lists = function
| x1::tl1, x2::tl2 ->
(match compare x1 x2 with
Line 1,474 ⟶ 1,788:
| _ -> true)
| [], _ -> true
| _ -> false</langsyntaxhighlight>
 
Here is a small script to test this function:
 
<langsyntaxhighlight lang="ocaml">(* copy-paste the code of ordered_lists here *)
 
let make_num_list p n =
Line 1,497 ⟶ 1,811:
print_num_list lst1;
print_num_list lst2;
Printf.printf "ordered: %B\n" (ordered_lists (lst1, lst2))</langsyntaxhighlight>
 
Sample execution:
Line 1,507 ⟶ 1,821:
Also notice that the function <code>ordered_lists</code> will work with anything the function <code>Pervasives.compare</code> is able to compare (most OCaml types and structures made from the base types). In the prototype of this function below <code>'a list</code> means a list of anything:
 
<langsyntaxhighlight lang="ocaml">val ordered_lists : 'a list * 'a list -> bool</langsyntaxhighlight>
 
=={{header|Oforth}}==
Line 1,521 ⟶ 1,835:
=={{header|Ol}}==
This sample very similar to Scheme, but implements proper tail recursion. So can test unlimited length lists.
<langsyntaxhighlight lang="scheme">
(define (lexorder a b)
(cond
Line 1,536 ⟶ 1,850:
(print (lexorder '(1 2 3) '(1 2 3))) ; => false
(print (lexorder '(1 2 3) '(1 2 8))) ; => true
</syntaxhighlight>
</lang>
 
=={{header|PARI/GP}}==
<syntaxhighlight lang ="parigp">lex(u,v)<1</langsyntaxhighlight>
 
=={{header|Perl}}==
<langsyntaxhighlight Perllang="perl">use strict;
use warnings;
 
Line 1,579 ⟶ 1,893:
my $before = orderlists([@$first], [@$second]) ? 'true' : 'false';
print "(@$first) comes before (@$second) : $before\n";
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,591 ⟶ 1,905:
=={{header|Phix}}==
Handled natively, eg ("?" is the shorthand print operator)
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>?{1,2,3}<{1,2,3,4} -- 1
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
?{1,2,3,4}<{1,2,3} -- 0
<span style="color: #0000FF;">?{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">}<{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">}</span> <span style="color: #000080;font-style:italic;">-- 1 (true under p2js)</span>
?{1,2,4}<{1,2,3} -- 0
<span style="color: #0000FF;">?{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">}<{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">}</span> <span style="color: #000080;font-style:italic;">-- 0 (false "")</span>
?{1,2,3}<{1,2,3} -- 0
<span style="color: #0000FF;">?{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">}<{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">}</span> <span style="color: #000080;font-style:italic;">-- 0 (false "")</span>
?{1,2,3}<{1,2,4} -- 1</lang>
<span style="color: #0000FF;">?{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">}<{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">}</span> <span style="color: #000080;font-style:italic;">-- 0 (false "")</span>
<span style="color: #0000FF;">?{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">}<{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">}</span> <span style="color: #000080;font-style:italic;">-- 1 (true "")</span>
<!--</syntaxhighlight>-->
Elements can be any mix of integers, floating point numbers, strings, or nested subsequences, with atoms ordered before sequences.<br>
If you want -1/0/+1 (instead of the true(1)/false(0) shown above), use the builtin compare() function, and that will match under p2js.
 
=={{header|Phixmonti}}==
{{trans|Phix}}
<langsyntaxhighlight Phixmontilang="phixmonti">include Utilitys.pmt
 
def ? print nl enddef
Line 1,609 ⟶ 1,926:
( 1 2 4 ) ( 1 2 3 ) < ?
( 1 2 3 ) ( 1 2 3 ) < ?
( 1 2 3 ) ( 1 2 4 ) < ?</langsyntaxhighlight>
 
=={{header|Picat}}==
Picat has a built-in general comparison operator <code>@<</code>, which compares terms in lexicographic order (numeric lists as well as atoms, strings, variables, and structures).
<syntaxhighlight lang="picat">go =>
Tests = [
[[1,2,3], [1,2,3]],
[[1,2,3], [1,2,3,4]],
[[1,2,2], [1,2,3]],
[[1,2,4], [1,2,3]],
[1..10 , 1..8],
[[] , []],
[[] , [1]],
[[1] , [] ],
[[-1,2,3,6,5],[-1,2,3,5,6]],
[[-1,2,3,-5,6],[-1,2,3,-6,5]]
],
foreach([L1,L2] in Tests)
printf("%w @< %w: %w\n",L1,L2,cond(L1 @< L2,true,false))
end,
nl.</syntaxhighlight>
 
In the Picat shell, the result is yes/no rather than true/false:
<pre>
Picat> [1,2,3] @< [1,2,3]
 
no
 
Picat> [1,2,3] @< [1,2,3,4]
yes</pre>
 
=={{header|PicoLisp}}==
The built-in comparison functions already do this (not only for lists of numbers, but for any arbitrary data type).
<langsyntaxhighlight PicoLisplang="picolisp">: (> (1 2 0 4 4 0 0 0) (1 2 1 3 2))
-> NIL</langsyntaxhighlight>
 
=={{header|Pike}}==
<langsyntaxhighlight Pikelang="pike">int(0..1) order_array(array a, array b)
{
if (!sizeof(a)) return true;
Line 1,624 ⟶ 1,971:
return order_array(a[1..], b[1..]);
return a[0] < b[0];
}</langsyntaxhighlight>
Pikes <code>Array.sort_array()</code> function can sort an array of arrays using the <code><</code> operator, but it will sort longer arrays before shorter ones. Therefore the above function is still needed if the intent is to use the comparison for a sort operation.
 
If the numbers are in 32bit signed integer range, the following works too:
<langsyntaxhighlight Pikelang="pike">(string)a < (string)b;</langsyntaxhighlight>
 
=={{header|PL/I}}==
<langsyntaxhighlight PLlang="pl/Ii">lists: procedure options (main); /* 8 June 2014 */
 
declare a(10) fixed initial (1, 2, 3, 4, 5, 8, 9, 10, 16, 17),
Line 1,652 ⟶ 1,999:
end compare;
 
end lists;</langsyntaxhighlight>
Results:
<pre>
Line 1,661 ⟶ 2,008:
 
=={{header|PowerShell}}==
<syntaxhighlight lang="powershell">
<lang PowerShell>
function order($as,$bs) {
if($as -and $bs) {
Line 1,671 ⟶ 2,018:
}
"$(order @(1,2,1,3,2) @(1,2,0,4,4,0,0,0))"
</syntaxhighlight>
</lang>
<b>Output:</b>
<pre>
Line 1,678 ⟶ 2,025:
 
===Non-Recursive Version===
<syntaxhighlight lang="powershell">
<lang PowerShell>
function Test-Order ([int[]]$ReferenceArray, [int[]]$DifferenceArray)
{
Line 1,695 ⟶ 2,042:
return ($ReferenceArray.Count -lt $DifferenceArray.Count) -or (Compare-Object $ReferenceArray $DifferenceArray) -eq $null
}
</syntaxhighlight>
</lang>
<syntaxhighlight lang="powershell">
<lang PowerShell>
Test-Order -ReferenceArray 1, 2, 1, 3, 2 -DifferenceArray 1, 2, 0, 4, 4, 0, 0, 0
Test-Order -ReferenceArray 1, 2, 1, 3, 2 -DifferenceArray 1, 2, 2, 4, 4, 0, 0, 0
Line 1,702 ⟶ 2,049:
Test-Order -ReferenceArray 1, 2 -DifferenceArray 1, 2, 3
Test-Order -ReferenceArray 1, 2 -DifferenceArray 1, 2
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 1,713 ⟶ 2,060:
 
=={{header|PureBasic}}==
<langsyntaxhighlight lang="purebasic">DataSection
Array_1:
Data.i 5 ;element count
Line 1,783 ⟶ 2,130:
CloseConsole()
EndIf
</langsyntaxhighlight>
{{out}}
<pre>False
Line 1,793 ⟶ 2,140:
=={{header|Python}}==
The built-in comparison operators already do this:
<langsyntaxhighlight lang="python">>>> [1,2,1,3,2] < [1,2,0,4,4,0,0,0]
False</langsyntaxhighlight>
 
=={{header|Quackery}}==
 
<langsyntaxhighlight Quackerylang="quackery"> [ [ over [] = iff false done
dup [] = iff true done
behead rot behead rot
Line 1,808 ⟶ 2,155:
' [ [ ] [ 1 ] [ 1 1 1 ] [ 1 2 ] [ 1 2 1 ] [ 2 ] [ 2 1 ] [ 2 1 1 ] ]
 
shuffle sortwith []< echo</langsyntaxhighlight>
 
{{out}}
Line 1,816 ⟶ 2,163:
 
=={{header|Racket}}==
<langsyntaxhighlight Racketlang="racket">#lang racket
 
(define (lex<? a b)
Line 1,825 ⟶ 2,172:
 
(lex<? '(1 2 3 4 5) '(1 2 3 4 4)) ; -> #f
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
There is already a built-in comparison operator.
<syntaxhighlight lang="raku" perl6line>my @a = <1 2 4>;
my @b = <1 2 4>;
say @a," before ",@b," = ", @a before @b;
Line 1,846 ⟶ 2,193:
my @b = flat @a.map: { Bool.pick ?? $_ !! (^100).roll((0..2).pick) }
say @a," before ",@b," = ", @a before @b;
}</langsyntaxhighlight>
{{out}}
<pre>1 2 4 before 1 2 4 = False
Line 1,864 ⟶ 2,211:
=={{header|Rascal}}==
The built-in comparison operator already does this:
<langsyntaxhighlight lang="rascal">rascal>[2,1,3] < [5,2,1,3]
bool: true</langsyntaxhighlight>
 
=={{header|REXX}}==
Line 1,871 ⟶ 2,218:
 
This example will also work with non-numeric strings.
<langsyntaxhighlight lang="rexx">/*REXX program determines if a list < previous list, and returns true or false. */
@.=; @.1 = 1 2 1 5 2
@.2 = 1 2 1 5 2 2
Line 1,894 ⟶ 2,241:
end /*k*/
if wx<wy then return 'true' /*handle case of equal (so far). */
return 'false' /* " " " " " " */</langsyntaxhighlight>
{{out|output|:}}
<pre>
Line 1,905 ⟶ 2,252:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
list1 = "1, 2, 1, 5, 2"
list2 = "5, 2, 1, 5, 2, 2"
Line 1,925 ⟶ 2,272:
func order alist, blist
return strcmp(alist, blist)
</syntaxhighlight>
</lang>
Output:
<pre>
Line 1,931 ⟶ 2,278:
list2>list3
list3=list4
</pre>
 
=={{header|RPL}}==
{{works with|HP|28}}
{| class="wikitable" ≪
! RPL code
! Comment
|-
|
≪ → a b
≪ a SIZE b SIZE MIN LAST <
1 ROT '''FOR''' j
'''IF''' b j GET a j GET - '''THEN'''
LAST 0 > SWAP DROP
a SIZE 'j' STO
'''END'''
'''NEXT'''
≫ ≫ ‘<span style="color:blue">GR8R?</span>’ STO
|
<span style="color:blue">GR8R?</span> ''( [reals] [reals] → boolean ) ''
m = min(size(a),size(b)) ; res = (size(a)<size(b))
loop for j=1 to m
if b[j]-a[j] ≠ 0
res = (b[j]-a[j]>0)
break
end if
end loop, return res
.
|}
[1 2 1 5 2] [1 2 1 5 2 2] <span style="color:blue">GR8R?</span>
[1 2 1 5 2 2] [1 2 1 5 2] <span style="color:blue">GR8R?</span>
[1 2 1 5 2 2] [1 2 3 4 5] <span style="color:blue">GR8R?</span>
[1 2 3 4 5] [1 2 1 5 2 2] <span style="color:blue">GR8R?</span>
[1 2 3 4 5] [1 2 3 4 5] <span style="color:blue">GR8R?</span>
{{out}}
<pre>
5: 1
4: 0
3: 1
2: 0
1: 0
</pre>
 
=={{header|Ruby}}==
The built-in <code><=></code> operator already does this:
<langsyntaxhighlight lang="ruby">>> ([1,2,1,3,2] <=> [1,2,0,4,4,0,0,0]) < 0
=> false</langsyntaxhighlight>
 
=={{header|Rust}}==
<code>Vec<T></code> implements <code>Ord</code> when <code>T</code> does, so we can just compare them with <code><</code>. (Same with arrays and slices).
<langsyntaxhighlight Rustlang="rust">vec![1, 2, 1, 3, 2] < vec![1, 2, 0, 4, 4, 0, 0, 0]</langsyntaxhighlight>
 
=={{header|Scala}}==
<langsyntaxhighlight Scalalang="scala">def lessThan1(a: List[Int], b: List[Int]): Boolean =
if (b.isEmpty) false
else if (a.isEmpty) true
else if (a.head != b.head) a.head < b.head
else lessThan1(a.tail, b.tail)</langsyntaxhighlight><syntaxhighlight lang Scala="scala">def lessThan2(a: List[Int], b: List[Int]): Boolean = (a, b) match {
case (_, Nil) => false
case (Nil, _) => true
case (a :: _, b :: _) if a != b => a < b
case _ => lessThan2(a.tail, b.tail)
}</langsyntaxhighlight><syntaxhighlight lang Scala="scala">def lessThan3(a: List[Int], b: List[Int]): Boolean =
a.zipAll(b, Integer.MIN_VALUE, Integer.MIN_VALUE)
.find{case (a, b) => a != b}
.map{case (a, b) => a < b}
.getOrElse(false)</langsyntaxhighlight><syntaxhighlight lang Scala="scala">val tests = List(
(List(1, 2, 3), List(1, 2, 3)) -> false,
(List(3, 2, 1), List(3, 2, 1)) -> false,
Line 1,969 ⟶ 2,357:
assert(lessThan2(a, b) == c, test)
assert(lessThan3(a, b) == c, test)
}</langsyntaxhighlight>
 
=={{header|Scheme}}==
<langsyntaxhighlight lang="scheme">(define (lex<? a b)
(cond ((null? b) #f)
((null? a) #t)
((= (car a) (car b)) (lex<? (cdr a) (cdr b)))
(else (< (car a) (car b)))))</langsyntaxhighlight>
 
=={{header|Seed7}}==
The operator corresponding to the ordering described in this example is less than.
 
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
 
const proc: main is func
Line 1,993 ⟶ 2,381:
writeln([] (1) < 0 times 0); # Any nonempty list is not less than the empty list --> FALSE
writeln(0 times 0 < 0 times 0); # The empty list is not less than the empty list --> FALSE
end func;</langsyntaxhighlight>
 
{{out}}
Line 2,009 ⟶ 2,397:
=={{header|Sidef}}==
Built-in, via the comparison operator (`<=>`):
<langsyntaxhighlight lang="ruby">func ordered(a, b) {
(a <=> b) < 0
}
Line 2,022 ⟶ 2,410:
var before = ordered(a, b)
say "#{a} comes before #{b} : #{before}"
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,031 ⟶ 2,419:
 
=={{header|Standard ML}}==
<langsyntaxhighlight lang="sml">- List.collate Int.compare ([1,2,1,3,2], [1,2,0,4,4,0,0,0]) = LESS;
val it = false : bool</langsyntaxhighlight>
 
=={{header|Swift}}==
<langsyntaxhighlight lang="swift">let a = [1,2,1,3,2]
let b = [1,2,0,4,4,0,0,0]
println(lexicographicalCompare(a, b)) // this is "less than"</langsyntaxhighlight>
{{out}}
<pre>
Line 2,044 ⟶ 2,432:
 
=={{header|Tcl}}==
<langsyntaxhighlight lang="tcl">proc numlist< {A B} {
foreach a $A b $B {
if {$a<$b} {
Line 2,053 ⟶ 2,441:
}
return 0
}</langsyntaxhighlight>
 
=={{header|TUSCRIPT}}==
<langsyntaxhighlight lang="tuscript">
$$ MODE TUSCRIPT
MODE DATA
Line 2,081 ⟶ 2,469:
list1=VALUE(list2)
ENDLOOP
</syntaxhighlight>
</lang>
{{out}}
<pre style='height:30ex;overflow:scroll'>
Line 2,097 ⟶ 2,485:
 
=={{header|VBA}}==
<langsyntaxhighlight lang="vb">Private Function order(list1 As Variant, list2 As Variant) As Boolean
i = 1
Do While list1(i) <= list2(i)
Line 2,116 ⟶ 2,504:
Debug.Print order([{1, 2, 3, 4}], [{1,2,3}])
Debug.Print order([{1, 2, 3}], [{1,2,3,4}])
End Sub</langsyntaxhighlight>{{out}}
<pre>Onwaar
Onwaar
Line 2,122 ⟶ 2,510:
 
=={{header|VBScript}}==
<syntaxhighlight lang="vb">
<lang vb>
Function order_list(arr1,arr2)
order_list = "FAIL"
Line 2,156 ⟶ 2,544:
WScript.StdOut.WriteLine order_list(Array(0,0),Array(1))
WScript.StdOut.WriteLine order_list(Array(1,2,1,3,2),Array(1,2,0,4,4,0,0,0))
</syntaxhighlight>
</lang>
 
{{Out}}
Line 2,174 ⟶ 2,562:
=={{header|Wart}}==
We'll simply overload <code>&lt;</code> for lists.
<langsyntaxhighlight lang="python">def (a < b) :case (or list?.a list?.b)
if not.b
nil
Line 2,182 ⟶ 2,570:
(cdr.a < cdr.b)
:else
(car.a < car.b)</langsyntaxhighlight>
 
{{out}}
Line 2,196 ⟶ 2,584:
 
=={{header|Wren}}==
<langsyntaxhighlight ecmascriptlang="wren">var orderLists = Fn.new { |l1, l2|
var len = (l1.count <= l2.count) ? l1.count : l2.count
for (i in 0...len) {
Line 2,221 ⟶ 2,609:
var res = orderLists.call(lists[i], lists[i+1])
System.print("list[%(i)] < list[%(i+1)] -> %(res)")
}</langsyntaxhighlight>
 
{{out}}
Line 2,241 ⟶ 2,629:
list[5] < list[6] -> true
list[6] < list[7] -> true
</pre>
 
=={{header|XPL0}}==
{{trans|ALGOL W}}
<syntaxhighlight lang "XPL0"> \Compare lists (rows) of integers.
\Returns TRUE if there is an element in A that is < the corresponding
\ element in B and all previous elements are equal, FALSE otherwise.
\The bounds of A and B should ALB :: AUB and BLB :: BUB.
function ILT ( A, ALB, AUB, B, BLB, BUB );
integer A, ALB, AUB, B, BLB, BUB;
integer APos, BPos, Equal;
begin
APos := ALB;
BPos := BLB;
Equal := true;
while APos <= AUB and BPos <= BUB and Equal do begin
Equal := A( APos ) = B( BPos );
if Equal then begin
APos := APos + 1;
BPos := BPos + 1
end \if_Equal
end; \while_more_elements_and_Equal
if not Equal
then \there is an element in A and B that is not Equal
return A( APos ) < B( BPos )
else \all elements are Equal or one list is shorter
\A is < B if A has fewer elements
return APos > AUB and BPos <= BUB
end; \ILT
 
\Tests A < B has the expected result
procedure Test ( AName, A, ALB, AUB, BName, B, BLB, BUB, Expected );
integer AName, A, ALB, AUB, BName, B, BLB, BUB, Expected;
integer IsLt;
begin
IsLt := ILT( A, ALB, AUB, B, BLB, BUB );
Text(0, AName);
Text(0, if IsLt then " < " else " >= ");
Text(0, BName);
Text(0, if IsLt = Expected then " " else ", NOT as expected");
CrLf(0);
end; \test
 
integer List1, List2, List3, List4, List5, List6, List7, List8;
begin
\test cases as in the BBC basic sample
List1 := [0, 1, 2, 1, 5, 2];
List2 := [0, 1, 2, 1, 5, 2, 2];
List3 := [0, 1, 2, 3, 4, 5];
List4 := [0, 1, 2, 3, 4, 5];
Test( "List1", List1, 1, 5, "List2", List2, 1, 6, true );
Test( "List2", List2, 1, 6, "List3", List3, 1, 5, true );
Test( "List3", List3, 1, 5, "List4", List4, 1, 5, false );
\additional test cases
List5 := [0, 9, 0, 2, 1, 0];
List6 := [0, 4, 0, 7, 7];
List7 := [0, 4, 0, 7];
List8 := [0, 0];
Test( "List5", List5, 1, 5, "List6", List6, 1, 4, false );
Test( "List6", List6, 1, 4, "List7", List7, 1, 3, false );
Test( "List7", List7, 1, 3, "List8", List8, 1, 0, false );
Test( "List8", List8, 1, 0, "List7", List7, 1, 3, true )
end</syntaxhighlight>
{{out}}
<pre>
List1 < List2
List2 < List3
List3 >= List4
List5 >= List6
List6 >= List7
List7 >= List8
List8 < List7
</pre>
 
=={{header|Yabasic}}==
{{trans|FreeBASIC}}
<syntaxhighlight lang="yabasic">
read num : dim list1(4)
read num : dim list2(5)
read num : dim list3(4)
read num : dim list4(4)
 
if Orden(list1(), list2()) then print "list1 < list2" else print "list1 >= list2" : fi
if Orden(list2(), list3()) then print "list2 < list3" else print "list2 >= list3" : fi
if Orden(list3(), list4()) then print "list3 < list4" else print "list3 >= list4" : fi
end
 
sub Orden(listA(), listB())
i = 0
l1 = arraysize(listA(), 1)
l2 = arraysize(listB(), 1)
while listA(i) = listB(i) and i < l1 and i < l2
i = i + 1
wend
if listA(i) < listB(i) then return True : fi
if listA(i) > listB(i) then return False : fi
return l1 < l2
end sub
 
data 1, 2, 1, 5, 2
data 1, 2, 1, 5, 2, 2
data 1, 2, 3, 4, 5
data 1, 2, 3, 4, 5
</syntaxhighlight>
{{out}}
<pre>
Igual que la entrada de BBC BASIC.
</pre>
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">fcn listLT(a,b){
a.walker().zip(b).filter1(fcn([(a,b)]){ a<b }) : // lazy
if(_) return(True);;
a.len()<b.len()
}</langsyntaxhighlight>
{{out}}
<pre>
1,983

edits