ID:263562
 
I have alot of data that needs to be sorted often, so I decided to make a datum to do it. However it seems not to work with my test verb.
array
var
list/contents[0]
proc
Add(item)
if(contents.len == 0)
contents += item
else if(contents.len == 1)
if(item > contents[1])
contents += item
else
contents.Insert(1,item)
else
for(var/i = 1 to contents.len - 1)
var/current = contents[i]
var/next = contents[i+1]
if(next > item && current <= item)
contents.Insert(current,item)
break
mob
verb
test()
var/array/A = new
for(var/i = 1 to 1000)
A.Add(rand(1,i))
for(var/x in A.contents)
src << x
I'm not completely sure about how the [] operators work with lists in DM, but in C++ that would make an array with a max size of 0...

This is how I would do it.
var
list/contents = new/list()


That way its a dynamic array, so there is no practical limit to the size.
Hope that helps.

-KirbyAllStar
In response to KirbyAllStar
KirbyAllStar wrote:
I'm not completely sure about how the [] operators work with lists in DM, but in C++ that would make an array with a max size of 0...

var/list/L[0] is the equivalent of var/list/L=new (which is the equivalent of var/list/L=new/list as you said).
In response to Android Data
Hmmm interesting, which is more efficient or are they the same?

-KirbyAllStar
In response to KirbyAllStar
Anyone still know why its not working?
In response to Xx Dark Wizard xX
Yep, I spent quite a while trying to figure it out, but I got it working.

Here's Yours
array
var
list/contents[0]
proc
Add(item)
if(contents.len == 0)
contents += item
else if(contents.len == 1)
if(item > contents[1])
contents += item
else
contents.Insert(1,item)
else
for(var/i = 1 to contents.len - 1)//This loop was your biggest problem... When I tested some stuff this was never running at all because contents.len was 2 and the loop tried going 1 to 1 lol
var/current = contents[i]
var/next = contents[i+1] //The way I have it, you don't need a next but in theory you could use it.
if(next > item && current <= item)
contents.Insert(current,item)
break
mob
verb
test()
var/array/A = new
for(var/i = 1 to 1000)
A.Add(rand(1,i))
for(var/x in A.contents)
src << x


And Mine
array
var
list/contents[0]
proc
Add(item)
if(contents.len)
if(item > contents[contents.len])
contents += item
else
for(var/i = 1 to contents.len)
var/current =contents[i]
if(item <= current)
contents.Insert(i,item)
break
else
contents += item

mob
verb
test()
var/array/A = new
for(var/i = 1 to 100)
A.Add(rand(1,i))
for(var/x in A.contents)
src << x


That is assuming that I understood what you wanted...You did want a sorted array right?

Thank you very much for giving me this challenge, I understand quite a bit more about lists in DM now.

Hope it helps, good luck with whatever your using it for. If you have questions just ask

-KirbyAllStar
In response to KirbyAllStar
Thanks man, glad I helped you out a bit.
In response to Xx Dark Wizard xX
No Problem