[Tutor] Sorting [array slicing for copying arrays]

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Tue, 30 Jul 2002 23:40:17 -0700 (PDT)


> And how I do it now, which works:
>
>     def cmp_alpha1(self, x, y):
>         return cmp((x.last, x.first),
>                    (y.last, y.first))
>
>     def sort_alpha1(self):
>         self.results = []
>         for item in self.roster:
>             self.results.append(item)
>         self.results.sort(self.cmp_alpha1)
>         return self.results
>
> But, more importantly, I understand how the new way works.  And, it is
> much cleaner and easier to understand.

There's always room for improvement.  *grin* You can copy the roster in
one shot by using a whole array slice, like this:

    self.results = self.roster[:]


I hope this helps!