How to: sort some integer values (without use of Temp-Table)

Posted by DenDuze on 18-Jun-2015 10:27

Hi,


I want to sort some integer values.
I found that there exists a System.Array object that has some sort method so that looks like a good way to do this (that way I don't need to make some function/procedure that sorts these values or use a temp-table)

Has someone experience with the sort-method and could me give some example in Progress how to do this.
I already tried mutiple things but or I get some strange (error)-messages or nothing happens.


My last try was like below but I do not get the numbers back after the sort-method (it's a combination of things I found but always I have some problem. Also is the code below for string because when I change to Int16 I get other errors)

define variable someString as "System.string[]" no-undo.

someString = new "System.String[]" (3).

someString:setValue("12", 0).
someString:setValue("3", 1).
someString:setValue("16", 2).
                                       
System.Array:Sort(Somestring).

/* this just gives me System.String[] */
message SomeString:Tostring() view-as alert-box.

DEFINE VARIABLE i AS INTEGER     NO-UNDO.
do i = 0 to someString:length - 1:

  /* this also gives me just System.String[] */
  MESSAGE somestring:ToString()
          VIEW-AS ALERT-BOX INFO BUTTONS OK.
end.

/* both of these just gives me the value of the array back but not sorted like I wanted */
message UNBOX (someString:GetValue (0)) skip
        someString:GetValue (1)
  VIEW-AS ALERT-BOX INFO BUTTONS OK. 

So maybe someone can help with this

Posted by Brian K. Maher on 18-Jun-2015 12:20

I would go with this instead of an array...

define variable MySortedList as System.Collections.SortedList.

define variable i as integer.

MySortedList = new System.Collections.SortedList().

MySortedList:Add(32,32).
MySortedList:Add(12,12).
MySortedList:Add(7,7).


do i = 0 to MySortedList:Count - 1:
    message MySortedList:GetByIndex(i):ToString() view-as alert-box.
end.

Posted by Brian K. Maher on 18-Jun-2015 12:44

This would also work:

define variable someString as "System.Int32[]" no-undo.

someString = new "System.Int32[]" (3).

someString:SetValue(12,0).
someString:setValue(3, 1).
someString:setValue(16, 2).

System.Array:Sort(Somestring).

/* this just gives me System.String[] */
message SomeString:Tostring() view-as alert-box.

DEFINE VARIABLE i AS INTEGER NO-UNDO.
do i = 0 to someString:length - 1:

/* this also gives me just System.String[] */
MESSAGE somestring:GetValue(i)
VIEW-AS ALERT-BOX INFO BUTTONS OK.
end.

/* both of these just gives me the value of the array back but not sorted like I wanted */
message UNBOX (someString:GetValue (0)) skip
someString:GetValue (1)
VIEW-AS ALERT-BOX INFO BUTTONS OK.

All Replies

Posted by TheMadDBA on 18-Jun-2015 11:09

Not sure why you want to avoid using a temp-table since it would much easier (and portable).

But... if you want the string to sort in numeric order you will have to pad the values with zeros. So use 12,03 and 16 (for an example).

Posted by DenDuze on 18-Jun-2015 11:53

I do not want to sort strings as numbers!

The strings in this example are just because I could not manage to make this simple example with integers ;-(

So I would like to provide some numbers to the object, use the sort-method and then do something ith that sorted result.

Posted by Brian K. Maher on 18-Jun-2015 12:20

I would go with this instead of an array...

define variable MySortedList as System.Collections.SortedList.

define variable i as integer.

MySortedList = new System.Collections.SortedList().

MySortedList:Add(32,32).
MySortedList:Add(12,12).
MySortedList:Add(7,7).


do i = 0 to MySortedList:Count - 1:
    message MySortedList:GetByIndex(i):ToString() view-as alert-box.
end.

Posted by OctavioOlguin on 18-Jun-2015 12:32

You tell that you want to sort integer, but use string at the end?

Take it as an act of faith:

/* somehow magically, sort 3 _strings_ */

DEFINE TEMP-TABLE tSort
FIELD stub AS char
INDEX pk IS PRIMARY  stub ASCENDING.

CREATE tSort.
ASSIGN stub = "12".
CREATE tSort.
ASSIGN stub = "03".
CREATE tSort.
ASSIGN stub = "16".

FOR EACH tSort BY stub:
    MESSAGE stub
    VIEW-AS ALERT-BOX.
END.    


/* that's it */

my humble opinion

Posted by Brian K. Maher on 18-Jun-2015 12:44

This would also work:

define variable someString as "System.Int32[]" no-undo.

someString = new "System.Int32[]" (3).

someString:SetValue(12,0).
someString:setValue(3, 1).
someString:setValue(16, 2).

System.Array:Sort(Somestring).

/* this just gives me System.String[] */
message SomeString:Tostring() view-as alert-box.

DEFINE VARIABLE i AS INTEGER NO-UNDO.
do i = 0 to someString:length - 1:

/* this also gives me just System.String[] */
MESSAGE somestring:GetValue(i)
VIEW-AS ALERT-BOX INFO BUTTONS OK.
end.

/* both of these just gives me the value of the array back but not sorted like I wanted */
message UNBOX (someString:GetValue (0)) skip
someString:GetValue (1)
VIEW-AS ALERT-BOX INFO BUTTONS OK.

Posted by TheMadDBA on 18-Jun-2015 12:45

+1 Octavio :-)

Den I was pointing out that your list was being sorted... just sorted alphabetically. Adding the zeroes to the front sorts it "properly". Just like if you take the example that Octavio posted and remove the leading zeros...

Posted by Brian K. Maher on 18-Jun-2015 12:48

Sorry bit I would give this a -1 as the answer is not what Den is asking for.
 
[collapse]
From: TheMadDBA [mailto:bounce-TheMadDBA@community.progress.com]
Sent: Thursday, June 18, 2015 1:46 PM
To: TU.OE.Development@community.progress.com
Subject: RE: [Technical Users - OE Development] How to: sort some integer values (without use of Temp-Table)
 
Reply by TheMadDBA

+1 Octavio :-)

Den I was pointing out that your list was being sorted... just sorted alphabetically. Adding the zeroes to the front sorts it "properly". Just like if you take the example that Octavio posted and remove the leading zeros...

Stop receiving emails on this subject.

Flag this post as spam/abuse.

[/collapse]

Posted by TheMadDBA on 18-Jun-2015 13:00

I agree that the Int32 version that you posted is the proper answer based on what Den was asking for.

I personally just think that doing the system.* approach for a simple sort is more complicated and harder to maintain than just using a temp-table. Especially once you actually start trying to use the sorted results. But that is just my opinion obviously.

Posted by DenDuze on 18-Jun-2015 13:44

@Brian: thanks, these both suggestions do the job.

I did not know about such a System.Collections.SortedList.

The sollution with the Int32 also does what I want (damn I was close to this sollution, I tried a Int16 but I've got an error when I runned that code

So the only thing that I've done wrong was the use of Int16 instead of Int32

I tried your code again with Int16 and indeed I get the same error "System.ArgumentExeption: cannot widen from source type to target type ....".

I did not really understand what this means (I'm new to that .net stuff) so then I tried with a String to check if the code was ok but that also did not work so then I asked help with this topic.

Thanks!!

Still 2 questions:

- why does this not work with Int16?

- what is the better way: SortedList or Int32 (or is there no best way - just a way to do it)

Posted by DenDuze on 18-Jun-2015 13:51

@TheMadDBA: Why do you find the System approach more complicated then using a Temp-Table?

A Temp-Table you have to define, fill and for-each

With the system you just create the object, assign the values, call some methods

I have to say that I prefer the System approach in my case (but that is a personal preference)

I have to admit that with a Temp-Table I would have coded this quicker (but that is the fault of my .Net knowledge).

Posted by Brian K. Maher on 18-Jun-2015 13:57

Den,

System.Int16 is a valid data type in .NET and it compiled in my sample code but at runtime there was an error about widening the data value.

I /could/ look into that but honestly there is no real reason to.  The ABL itself does not have a 16 bit integer value and you are talking about saving a whopping 2 bytes for each integer value.  If that makes the difference between acceptable and unacceptable performance you have /much/ greater problems.  <smile>

Brian

Posted by TheMadDBA on 18-Jun-2015 14:01

I am going to out on a limb and say the int32 matches the OE integer and the int16 has no real counterpart on the OE side.

System.Int16 = -32,768 to 32,767

System.Int32 = -2,147,483,648 to 2,147,483,647 (matches OE integer)

Posted by Brian K. Maher on 18-Jun-2015 14:01

Den,

The SortedList vs Int32 are just options.  It all depends on what you want to do with the data.  Which one is better is really up to you and your requirements.

I like the SortedList because the Add method uses two parameters.  The first is the key (which in your case would just be the integer value).  The second can be any object and this could be used to store data related to the key value (i.e. a class instance).

But .. again .. it all really depends on your application needs.

Brian

Posted by Thomas Mercer-Hursh on 18-Jun-2015 14:08

"Any" object ... even an ABL object?

Posted by TheMadDBA on 18-Jun-2015 14:13

With the system approach you still have to define, assign and loop. So I don't really see the savings.

Plus the next time somebody has to touch your code they now have to learn the system approach if they don't know it. May not matter depending on the size of your shop and how much turnover you have.

Until we have .NET libraries for Unix you have a porting problem.

All that being said... you can code your application how you like and it is always good to learn new things.

You could always make a class to handle the sorting and hide the approach from people using the class (no matter which one you choose).

Posted by Brian K. Maher on 18-Jun-2015 14:20

As long as the ABL object includes "INHERITS System.Object" in the CLASS statement then yes.  Absent that then no.  :-)

Posted by Mike Fechner on 18-Jun-2015 14:22

So not available on AppServers on UNIX or PASOE.
Von: Brian K. Maher [mailto:bounce-maher@community.progress.com]
Gesendet: Donnerstag, 18. Juni 2015 21:21
An: TU.OE.Development@community.progress.com
Betreff: RE: [Technical Users - OE Development] How to: sort some integer values (without use of Temp-Table)
 
Reply by Brian K. Maher
As long as the ABL object includes "INHERITS System.Object" in the CLASS statement then yes.  Absent that then no.  :-)
Stop receiving emails on this subject.

Flag this post as spam/abuse.

Posted by Brian K. Maher on 18-Jun-2015 14:35

 
>> So not available on AppServers on UNIX or PASOE.
 
Actually, not available on any UNIX client (as should be obvious to everyone).
 
Not available on PASOE for now.
 
And .. neither is a concern to this customer since he is already using the .NET classes.
 
 
 
 

Posted by Mike Fechner on 18-Jun-2015 14:49

Isn't the AppServer the only UNIX client we care about :-)

Just kidding, you are right, Brian!

Von meinem Windows Phone gesendet

Von: Brian K. Maher
Gesendet: ‎18.‎06.‎2015 15:36
An: TU.OE.Development@community.progress.com
Betreff: RE: [Technical Users - OE Development] AW: How to: sort some integer values (without use of Temp-Table)

Reply by Brian K. Maher
 
>> So not available on AppServers on UNIX or PASOE.
 
Actually, not available on any UNIX client (as should be obvious to everyone).
 
Not available on PASOE for now.
 
And .. neither is a concern to this customer since he is already using the .NET classes.
 
 
 
 
Stop receiving emails on this subject.

Flag this post as spam/abuse.

Posted by TheMadDBA on 18-Jun-2015 15:00

Paging Tom B to the thread.... He loves his CHUI clients :-)

Posted by Mike Fechner on 18-Jun-2015 15:03

Only because he knows no better :-)

Von meinem Windows Phone gesendet

Von: TheMadDBA
Gesendet: ‎18.‎06.‎2015 16:01
An: TU.OE.Development@community.progress.com
Betreff: RE: [Technical Users - OE Development] How to: sort some integer values (without use of Temp-Table)

Reply by TheMadDBA

Paging Tom B to the thread.... He loves his CHUI clients :-)

Stop receiving emails on this subject.

Flag this post as spam/abuse.

Posted by Thomas Mercer-Hursh on 18-Jun-2015 15:06

Not quite true any more ... ProTop has a GUI version ...

Posted by ChUIMonster on 18-Jun-2015 15:48

My ears flopped over as soon as the thread turned to .NET and all hope of portability was lost.

TheMadDBA got it right when he suggested encapsulating the approach in a (Progress 4gl) class.  That way the implementation doesn't matter and it can be portable when you need it to be.

Posted by DenDuze on 19-Jun-2015 00:28

My question has nothing to do with acceptable or not ;-)

As a beginner with those .Net things I just took the smallest Int# to try this out.

But the I've got that strange error so I thought that there was something wrong @the way I was tryien to solve the problem.

Like you also said: it compiles without problems but @runtime you get that error...

For me the Int32 is also ok so ...

That the Int32 has no Progress counterpart I can understand but why does it compiles then?? (that is only confusing)

Posted by olivier.dunemann on 19-Jun-2015 00:55

Another "pure ABL" approach (may be lighter than the temp-table based code):

DEFINE VARIABLE l AS LOGICAL NO-UNDO.
DEFINE VARIABLE cb AS INTEGER VIEW-AS COMBO-BOX SORT NO-UNDO.
 
FORM cb.

ASSIGN l = cb:ADD-LAST("3")
       l = cb:ADD-LAST("1")
       l = cb:ADD-LAST("2").

MESSAGE cb:LIST-ITEMS
    VIEW-AS ALERT-BOX INFO BUTTONS OK.


...My two cents

Posted by DenDuze on 19-Jun-2015 04:14

Ha, I also thought of that but I did not do that because I needed this pure in code (no GUI)

I did not know that OE would not complain if you use a view-as statement without GUI

This also works  (but is confusing due to the view-as)

Posted by Brian K. Maher on 19-Jun-2015 06:08

Den,

Int32 does have a counterpart in Progress.  It is called INTEGER.

I believe the exception you were seeing when using System.Int16 is caused by the shrinking (not widening) of the 32-bit integer (i.e. the literal numbers in your code) down to 16-bit.  That can cause loss of data and I expect .NET is pointing that out, although with a poorly written error message.

Brian

Posted by preethi.pulipate@gmail.com on 22-Jun-2015 06:17

how to: swap the two numbers with example

[collapse]
On Thu, Jun 18, 2015 at 8:58 PM, DenDuze <bounce-DenDuze@community.progress.com> wrote:
Thread created by DenDuze

Hi,


I want to sort some integer values.
I found that there exists a System.Array object that has some sort method so that looks like a good way to do this (that way I don't need to make some function/procedure that sorts these values or use a temp-table)

Has someone experience with the sort-method and could me give some example in Progress how to do this.
I already tried mutiple things but or I get some strange (error)-messages or nothing happens.


My last try was like below but I do not get the numbers back after the sort-method (it's a combination of things I found but always I have some problem. Also is the code below for string because when I change to Int16 I get other errors)

define variable someString as "System.string[]" no-undo.

someString = new "System.String[]" (3).

someString:setValue("12", 0).
someString:setValue("3", 1).
someString:setValue("16", 2).
                                       
System.Array:Sort(Somestring).

/* this just gives me System.String[] */
message SomeString:Tostring() view-as alert-box.

DEFINE VARIABLE i AS INTEGER     NO-UNDO.
do i = 0 to someString:length - 1:

  /* this also gives me just System.String[] */
  MESSAGE somestring:ToString()
          VIEW-AS ALERT-BOX INFO BUTTONS OK.
end.

/* both of these just gives me the value of the array back but not sorted like I wanted */
message UNBOX (someString:GetValue (0)) skip
        someString:GetValue (1)
  VIEW-AS ALERT-BOX INFO BUTTONS OK. 

So maybe someone can help with this

Stop receiving emails on this subject.

Flag this post as spam/abuse.




--
Thanks & Regards,
Preethi Pulipati.

[/collapse]

Posted by CMI on 22-Jun-2015 16:29

I know this is only a Bubble Sort but it's easy and it will work with INTEGERS and CHARACTER. 

function bubblesort returns character (input pcarray as character):
 
  define variable ctemparray as character no-undo.
  define variable i          as integer   no-undo.
  define variable j          as integer   no-undo.
  define variable ientries   as integer   no-undo.
 
  ientries = num-entries(pcarray).
 
  do i = ientries to 1 by -1:
    do j = 1 to i - 1:
      if entry(j, pcarray) > entry(j + 1, pcarray) then
      do:
        ctemparray            = entry(j, pcarray).
        entry(j, pcarray)     = entry(j + 1, pcarray).
        entry(j + 1, pcarray) = ctemparray.
      end.
    end.
  end.
 
  return pcarray.
 
end function.

message BubbleSort('9,8,7,6,5,4,3,2,1').

This thread is closed