473,698 Members | 2,102 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sorting on multiple values, some ascending, some descending

I have successfully used the sort lambda construct described in
http://mail.python.org/pipermail/pyt...il/377443.html.
However, how do I take it one step further such that some values can be
sorted ascending and others descending? Easy enough if the sort values
are numeric (just negate the value), but what about text?

Is the only option to define an external sorting function to loop
through the list and perform the comparisons one value at a time?

Jan 3 '07
11 18309
Neil Cerutti wrote:
Another trick is to factor the key application out of the sort.
This may be a good idea if when you want to minimize the number
of times your key function is called.

The idea is to mangle the list temporarily so you can use an
unkeyed sort, and then unmangle the sorted data. Here's a silly
example using a phone directory that's not stored in a format
that's easy to sort.
No need to jump through these hoops; list.sort(key=k eyfunc) calls keyfunc()
exactly once per list item:
>>from random import shuffle
items = range(-5, 10)
shuffle(items )
count = 0
def key(value):
.... global count
.... count += 1
.... return abs(value)
....
>>items.sort(ke y=key)
count
15

Peter
Jan 4 '07 #11
On 2007-01-04, Peter Otten <__*******@web. dewrote:
Neil Cerutti wrote:
>Another trick is to factor the key application out of the
sort. This may be a good idea if when you want to minimize the
number of times your key function is called.

The idea is to mangle the list temporarily so you can use an
unkeyed sort, and then unmangle the sorted data. Here's a
silly example using a phone directory that's not stored in a
format that's easy to sort.

No need to jump through these hoops; list.sort(key=k eyfunc)
calls keyfunc() exactly once per list item:
>>>from random import shuffle
items = range(-5, 10)
shuffle(item s)
count = 0
def key(value):
... global count
... count += 1
... return abs(value)
...
>>>items.sort(k ey=key)
count
15
Thanks for the correction! That's very useful information.

--
Neil Cerutti
Jan 4 '07 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2248
by: Henri Schomaecker | last post by:
Hi folks, does php 5 have a problem with html select lists which allow to choose multiple items? Whatever I tried I just can't get another or more than the last item selected in the list. Even if I select multiple items, a print_r($_POST) just shows the last selected item, and not a array of all selected values.
17
43938
by: Roland Hall | last post by:
Is there a way to return multiple values from a function without using an array? Would a dictionary object work better? -- Roland Hall /* This information is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a particular purpose. */ Technet Script Center - http://www.microsoft.com/technet/scriptcenter/ WSH 5.6 Documentation -...
5
2249
by: Homer Simpson | last post by:
Hi All, I'm trying to write a method where I pass three arguments and the method returns six values. All the values will be doubles. First, is it possible to get multiple values returned by a method? Second, how do I do it? I know how to write the method, pass arguments to it and get a single value returned but how do I pass multiple values back? Last, how do I reference the various values returned? I assume there is some array used...
1
5752
by: Mark | last post by:
Rather than have only one column sorted in a single direction in a listview. I would like to be able to sort on any column in alternating directions (ascending,descending) eg. First column click sorts ascending and second sorts decending etc. Can anyone point me in a direction? Thanks, Mark
16
17399
by: Nikolay Petrov | last post by:
How can I return multiple values from a custom function? TIA
7
3729
by: pt36 | last post by:
Hi I have a php string like this: $string = "one two three four five" I have to sorting the values randomly every time the page are loaded. So, for example: first time "one two three four five" second time "three five one four two" third time
1
2196
by: wendy184 | last post by:
I'm used to using 2007 which allows multiple values in the lookup wizard, this helps hugely with my queries as the database i'm building has information on one parent who may have up to 5 kids. Now i'm building up a similar database in 2000 and could really use some help, 2000 won't allow multiple values in a lookup field which means if I have a parent who has more than one kid I cant store the details, I have a seperate table for both parent...
2
3667
ADezii
by: ADezii | last post by:
The incentive for this Tip was an Article by the amazing Allen Browne - I considered it noteworthy enough to post as The Tip of the Week in this Access Forum. Original Article by Allen Browne Traditionally, one has always thought that Functions can only return a single value, and for the most part that was true. Ever since Access 95, we gained the new functionality, through VBA, to have Functions return an entire Structure of values. A User...
2
2178
by: satyanarayan sahoo | last post by:
Can we write a method which returns multiple values?
0
3098
by: Maric Michaud | last post by:
Le Thursday 28 August 2008 03:43:16 norseman, vous avez écrit : Disctionaries are hash tables with a unique key and constant time lookup. What you want could be implemented as a complex data structures with as many dict as needed keys, but it seems you really want a relational table and a rdbms. This is exactly what they are for. A short example with the new python2.5 sqlite package :
0
8668
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8598
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9014
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8885
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7708
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5857
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4612
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2320
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1995
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.