How to sort skipLists

Is it possible to sort a skiplist based on their key values?  Currently, when I insert items into a skiplist, they are not coming out in the same order they went in.  Instead, it comes out jumbled.  For example, if I add the numbers 1 through 10 to a skipList in order, when I loop through the same skip using for dataElement in skipList, they are displayed back to me in a jumbled order.  It might be: 3, 9, 7, 2, 1, 5, 10, 8, 4, 6.

 

Chris


chrscote - Tue Mar 06 11:15:18 EST 2018

Re: How to sort skipLists
davidcs - Tue Mar 06 11:39:24 EST 2018

Using a createString() skip the keys will sort alphabetically if that helps. This code:

Skip lSkip = createString()

put(lSkip, "c", "1")
put(lSkip, "d", "2")
put(lSkip, "e", "3")
put(lSkip, "b", "4")
put(lSkip, "a", "5")

string lVal 
for lTmp in lSkip do{
  string lKey = (string key lSkip)
  print lKey "\n"
}

 

Will always output as:
a
b
c
d
e

Re: How to sort skipLists
PekkaMakinen - Wed Mar 07 01:02:04 EST 2018

davidcs - Tue Mar 06 11:39:24 EST 2018

Using a createString() skip the keys will sort alphabetically if that helps. This code:

Skip lSkip = createString()

put(lSkip, "c", "1")
put(lSkip, "d", "2")
put(lSkip, "e", "3")
put(lSkip, "b", "4")
put(lSkip, "a", "5")

string lVal 
for lTmp in lSkip do{
  string lKey = (string key lSkip)
  print lKey "\n"
}

 

Will always output as:
a
b
c
d
e

CreateString will sort in alphabetical order, yes, but also the "normal" create Skip should be ordered if you use integers as keys. The problem with alphabetical ordering is that if you use numbers as keys, then 10, 11 and so on will be ordered before 2. Try the following out with "create" Skip or "createString" Skip.

 

Skip MySkip = create
int MaxNum = 100
int i = 0
 
for (i=0;i<MaxNum;i++)
   {
     put(MySkip, i, i)
   }
 
for i in MySkip do
{
   print i "\n"
}
 
delete MySkip

Re: How to sort skipLists
PekkaMakinen - Wed Mar 07 01:09:43 EST 2018

PekkaMakinen - Wed Mar 07 01:02:04 EST 2018

CreateString will sort in alphabetical order, yes, but also the "normal" create Skip should be ordered if you use integers as keys. The problem with alphabetical ordering is that if you use numbers as keys, then 10, 11 and so on will be ordered before 2. Try the following out with "create" Skip or "createString" Skip.

 

Skip MySkip = create
int MaxNum = 100
int i = 0
 
for (i=0;i<MaxNum;i++)
   {
     put(MySkip, i, i)
   }
 
for i in MySkip do
{
   print i "\n"
}
 
delete MySkip

And a proof that the integer skip list is really ordered by the key value

Skip MySkip = create
int MaxNum = 100
int i = 0
 
for (i=MaxNum;i>0;i--)
   {
     put(MySkip, i, i)
   }
 
for i in MySkip do
{
   print i "\n"
}
 
delete MySkip