Re: Sorting objects in ooRex? is this possible with data structures?



The sorting features are modeled off of what Java does with its Comparable and Comparator classes. If you're just sorting strings, then
you can just sort. If you want to sort on different column positions, we have a Comparator class that allows you to specify the comparison colums. For example,

myData~stableSortWith(.ColumnComparator~new(12, 6))

sorts on columns 12 through 6.

The default string comparison method is a strict string compare. If you'd wish to sort on numeric data, it's pretty simple to write your own comparator classes. Here's one that compares strings as numbers:

::class NumericComparator subclass Comparator
::method compare
use arg left, right
return (left - right)~sign -- the sign of the subtract gives order

Rick


geostat@xxxxxxxxxxxxx wrote:
On Sep 18, 5:03 am, Rick McGuire <object.r...@xxxxxxxxx> wrote:
rony wrote:

Lee Peedin wrote:
Coffee,
Currently the only sort available in ooRexx is SysStemSort and it
doesn't sort by fields, but column specification is possible, thus
each field in each element of the stem must be padded to a defined
length.
In ooRexx 3.2 there are several new sort methods coming. Maybe
Rick/David/Mark/Rony will come along and expound on them. Things are
happening so quick in ooRexx development that I've lost track of all
the new stuff.
You can get reference cards for ooRexx in PDF form from:
<http://wi.wu-wien.ac.at/rgf/rexx/misc/refCard/>
Look for "ooRexx-3-2-planned.pdf" for the upcoming 3.2.0 release (however that particular reference
card is subject to change depending on the changes applied to the next release of ooRexx).
---
There will be in essence two different sort algorithms available, a quickSort and a mergeSort, if I
am not mistaken. The quickSort will be used by the "sort" method, the somewhat resource hungrier
mergeSort will be used by the "stableSort" method. "stable" only means that the sequence of array
entries to sort remains unchanged, if the keys to sort after have already the same value, whereas in
the quickSort the sequence of array entries with the same key value may change (the result is still
sorted, so that's fine); the difference may only be relevant in cases where you have many duplicate
entries and want to have the same sequence of those duplicate entries in the result as well (rarely
needed, but extremely helpful, if needed).
The other way around. The stable mergesort is more resource hungry. If
it had not been, I'd probably only implemented the single sort version.







The sort methods are defined for the Array class.
E.g. to get a sorted list of all entries in the .local directory object you could do the following
with the upcoming version of ooRexx:
do idx over .local~allIndexes~sort
say idx
end
The above code will use the new method "allIndexes" (will be available for all collection
classes/datatpyes) to get an array of all available indexes (in this case of the .local directory
object). That array gets sorted. Iterating over this new, sorted array will allow to list the
entries in .local alphabetically.
The output of the current drop of the next version of ooRexx (just entered "do idx over
.local~allIndexes~sort;say idx;end" in a rexxtry session) yields:
ERROR
INPUT
LOCALSERVER
OUTPUT
STDERR
STDIN
STDOUT
STDQUE
This is sort done in its simplest form of just dealing with strings. To
accomplish what you want, you need to deep into some of the generalized
sorting features.

You have two choices to sort objects like you wish to do.

1) Implement a compareTo method as part of your object class.
compareTo is called by the sorting code for all comparisons between two
objects in the array. compareTo is responsible for returning a result
of -1, 0, 1 to indicate the relative ordering of the two objects.
Sorting of strings works because the String class implements a
compareTo() method in version 3.2.0.

2) Write a Comparator object and use the sortWith() method rather than
sort(). A comparator is responsible for comparing two object instances
to determine the relative order. Comparators are handy if you you have
multiple fields of your objects that you wish to sort on.

If you've used the Java Comparable/Comparator capabilities for sorting,
this should seem familiar. This was stolen^H^H^H^H^H^H modeled after
Java's sorting capabilities.

Rick





HTH,
---rony- Hide quoted text -
- Show quoted text -- Hide quoted text -

- Show quoted text -- Hide quoted text -

- Show quoted text -

Of interest to me is what features the new sorting function will
have. I am interested in two things
1. Sorting on numbers correctly
2. Sorting multiple fields up and down
Regards
Rex

.



Relevant Pages

  • Re: "Sorting" assignment
    ... Which would seem to imply that sort should be taught ... sorting is where many object-oriented languages fall down. ... Sorting really IS "matching all members of an array against, ... C's qsortinterface means that all is required is a custom comparator ...
    (comp.programming)
  • Re: How to sort String array A based on int array B
    ... String the other's an int. ... Then the Comparator has all it needs to sort -- the int. ...
    (comp.lang.java.help)
  • Re: indirect sort
    ... I would like to sort an array of object in respect to one of the ... Comparable in order to be able to choose the field I am sorting by) ... Maybe you could post your class and comparator code. ...
    (comp.lang.java.programmer)
  • comparator
    ... I am testing a Comparator with the PriorityQueue, ... added the number 100 or 1000 to the list, it did not sort right. ... public class NumberAscComparator implements Comparator ...
    (comp.lang.java.programmer)
  • Re: Any API to sort this Vector?
    ... > i.e. sort by first element then the second element. ... One way is to define a Comparator like the one below ... The comparator shown can be used on all arrays where the elements are ... Comparable, not just Strings. ...
    (comp.lang.java.programmer)