RosettaCodeData/Task/Sort-an-array-of-composite-structures/NetRexx/sort-an-array-of-composite-structures.netrexx
2023-07-01 13:44:08 -04:00

54 lines
2 KiB
Text

/* NetRexx */
options replace format comments java crossref symbols nobinary
-- =============================================================================
class RSortCompsiteStructure public
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method main(args = String[]) public static
places = [ -
PairBean('London', 'UK'), PairBean('New York', 'US') -
, PairBean('Boston', 'US'), PairBean('Washington', 'US') -
, PairBean('Washington', 'UK'), PairBean("Birmingham", 'US') -
, PairBean("Birmingham", 'UK'), PairBean("Boston", 'UK') -
]
say displayArray(places)
Arrays.sort(places, PairComparator())
say displayArray(places)
return
method displayArray(harry = PairBean[]) constant
disp = ''
loop elmt over harry
disp = disp','elmt
end elmt
return '['disp.substr(2)']' -- trim leading comma
-- =============================================================================
class RSortCompsiteStructure.PairBean
properties indirect
name
value
method PairBean(name_, value_) public
setName(name_)
setValue(value_)
return
method toString() public returns String
return '('getName()','getValue()')'
-- =============================================================================
class RSortCompsiteStructure.PairComparator implements Comparator
method compare(lft = Object, rgt = Object) public binary returns int
cRes = int
if lft <= RSortCompsiteStructure.PairBean, rgt <= RSortCompsiteStructure.PairBean then do
lName = String (RSortCompsiteStructure.PairBean lft).getName()
rName = String (RSortCompsiteStructure.PairBean rgt).getName()
cRes = lName.compareTo(rName)
if cRes == 0 then do
lVal = String (RSortCompsiteStructure.PairBean lft).getValue()
rVal = String (RSortCompsiteStructure.PairBean rgt).getValue()
cRes = lVal.compareTo(rVal)
end
end
else signal IllegalArgumentException('Arguments must be of type PairBean')
return cRes