Best way to sort a list?

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Message
Author
User avatar
xela
Lemma-Class Veteran
Posts: 2481
Joined: Sun Sep 18, 2011 10:13 am
Contact:

Re: Best way to sort a list?

#16 Post by xela »

Anima wrote:How about this?

Code: Select all

    class SortList(Action):
        def __init__(self, pList):
            self.list = pList
        def __call__(self):
            self.list.sort()
            return True
It sorts the list in place, so it doesn't change any references. As a plus point it also works for lists that aren't directly referenced from the store. Like lists that are referenced by an objects field.
Human should be dancing :)

But without sorting containers this is not even remotely useful to me, even with containers, I run most of my game logic from python classes and very rarely use any variables in store save the reference to instances of those classes.

One thing that has improved my Ren'Py coding experience instantly was this:

Code: Select all

    # Custom Screen Action classes:
    class Execute(Action):
        """
        Well... it executes... :)
        """
        def __init__(self, func, *args, **kwargs):
            self.func = func
            self.args = args
            self.kwargs = kwargs
        
        def __call__(self):
            self.func(*self.args, **self.kwargs)
            return "__"
I didn't expect adding new screen function could be so easy and did a lot of stupid things that could have been avoided otherwise :(
Like what we're doing? Support us at:
Image

Anima
Veteran
Posts: 448
Joined: Wed Nov 18, 2009 11:17 am
Completed: Loren
Projects: PS2
Location: Germany
Contact:

Re: Best way to sort a list?

#17 Post by Anima »

xela wrote:But without sorting containers this is not even remotely useful to me, even with containers, I run most of my game logic from python classes and very rarely use any variables in store save the reference to instances of those classes.
You know I don't have the slightest idea what you're trying to say here. Could you rephrase perhaps?
Avatar created with this deviation by Crysa
Currently working on:
  • Winterwolves "Planet Stronghold 2" - RPG Framework: Phase III

User avatar
xela
Lemma-Class Veteran
Posts: 2481
Joined: Sun Sep 18, 2011 10:13 am
Contact:

Re: Best way to sort a list?

#18 Post by xela »

xela wrote: Human should be dancing :)

But without sorting containers this is not even remotely useful to me, even with containers, I run most of my game logic from python classes and very rarely use any variables in store save the reference to instances of those classes.
Sorry, bad English. I meant to say that this is likely to be very useful to the original poster but it lacks ability to sort any iterables but lists.

Rest was not important.
Like what we're doing? Support us at:
Image

Anima
Veteran
Posts: 448
Joined: Wed Nov 18, 2009 11:17 am
Completed: Loren
Projects: PS2
Location: Germany
Contact:

Re: Best way to sort a list?

#19 Post by Anima »

The sorted function returns a list anyway, so I don't see the problem. You can simply convert whatever you want to sort into a list first.
Avatar created with this deviation by Crysa
Currently working on:
  • Winterwolves "Planet Stronghold 2" - RPG Framework: Phase III

User avatar
xela
Lemma-Class Veteran
Posts: 2481
Joined: Sun Sep 18, 2011 10:13 am
Contact:

Re: Best way to sort a list?

#20 Post by xela »

Anima wrote:You can simply convert whatever you want to sort into a list first.
Python developers made efforts so I don't have to, why would I waste it...

As far as lists go, your class completely solves the sorting problem.
Like what we're doing? Support us at:
Image

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot]