Hello all,
I am relatively new to python but I am having an issue with custom
sort functions..
I am trying to sort a list of lists or tuples with arbitrary ascending
or descending sorts. For example given a list of tuples
('firstname','lastname','age') I want to be able to sort lastname
descending, firstname ascending and age ascending...
I wrote a custom function generator to generate a sort function based
on an input list of column numbers and sort direction. Sort seems to
sort the first column ascending regardless of what the sort function
says. I also googled this group for other solutions and found a more
elegant one than mine but with the same results.
Here is my code and result:
sortList = [('2','D'),('1','D'),('0','D')]
dataList = []
dataList.append(['a','a','b'])
dataList.append(['a','a','a'])
dataList.append(['a','a','c'])
dataList.append(['a','b','a'])
dataList.append(['a','b','b'])
dataList.append(['a','b','c'])
dataList.append(['a','c','a'])
dataList.append(['a','c','b'])
outStr = 'def custSort( a, b):\n'
depth = 1
for sortPair in sortList:
indent = " "
curDent = depth * indent
outStr += curDent + 'if a[' + sortPair[0] + '] == b[' +
sortPair[0] + ']:\n'
depth += 1
depth -= 1
outStr += curDent + indent + 'return 0\n'
for j in range( len(sortList)-1, -1, -1 ):
curDent = depth * indent
print sortList[j][1]
if sortList[j][1] == 'A':
compareSym = '>'
elif sortList[j][1] == 'D':
compareSym = '<'
else:
print 'SORT DIRECTION ERROR ' + sortList[j][1]
outStr += curDent + 'elif a[' + sortList[j][0] + '] ' + compareSym
+ ' b[' + sortList[j][0] + ']:\n'
outStr += curDent + indent + 'return 1\n'
outStr += curDent + 'else:\n'
outStr += curDent + indent + 'return -1\n'
depth -= 1
print outStr
exec( outStr )
dataList.sort( custSort )
print str( dataList )
***************************
results:
D
D
D
def custSort( a, b):
if a[2] == b[2]:
if a[1] == b[1]:
if a[0] == b[0]:
return 0
elif a[0] < b[0]:
return 1
else:
return -1
elif a[1] < b[1]:
return 1
else:
return -1
elif a[2] < b[2]:
return 1
else:
return -1
[['a', 'b', 'c'], ['a', 'a', 'c'], ['a', 'c', 'b'], ['a', 'b', 'b'],
['a', 'a', 'b'], ['a', 'c', 'a'], ['a', 'b', 'a'], ['a', 'a', 'a']]
and Manuel Garcia's solution and results:
sortList = [(0,-1),(1,-1),(2,-1)]
dataList = []
dataList.append(['a','a','b'])
dataList.append(['a','a','a'])
dataList.append(['a','a','c'])
dataList.append(['a','b','a'])
dataList.append(['a','b','b'])
dataList.append(['a','b','c'])
dataList.append(['a','c','a'])
dataList.append(['a','c','b'])
def make_sort_f(list0):
def f(a,b):
for (i,m) in list0:
if a[i] == b[i]: continue
return m * cmp(a[i],b[i])
return 0
return f
dataList.sort( make_sort_f( sortList ) )
print str(dataList)
Results:
[['a', 'c', 'b'], ['a', 'c', 'a'], ['a', 'b', 'c'], ['a', 'b', 'b'],
['a', 'b', 'a'], ['a', 'a', 'c'], ['a', 'a', 'b'], ['a', 'a', 'a']]
is this an issue with sort or is my code screwy? Thanks in advance
for any help!
Ken R. 2 3323
"Ken R." <kr*****@hotmail.com> wrote in message
news:1e*************************@posting.google.co m... Hello all, I am relatively new to python but I am having an issue with custom sort functions..
Athough they seem to be working fine!
I am trying to sort a list of lists or tuples with arbitrary
ascending or descending sorts. For example given a list of tuples ('firstname','lastname','age') I want to be able to sort lastname descending, firstname ascending and age ascending...
I wrote a custom function generator to generate a sort function
based on an input list of column numbers and sort direction. Sort seems to sort the first column ascending regardless of what the sort function says. I also googled this group for other solutions and found a
more elegant one than mine but with the same results.
Given that your example data all have 'a' in the first column, these
statements of ill behavior make no sense!
Here is my code and result: sortList = [('2','D'),('1','D'),('0','D')] dataList = [] dataList.append(['a','a','b']) dataList.append(['a','a','a']) dataList.append(['a','a','c']) dataList.append(['a','b','a']) dataList.append(['a','b','b']) dataList.append(['a','b','c']) dataList.append(['a','c','a']) dataList.append(['a','c','b'])
You could just as well write dataList as a single literal.
[snip]
[['a', 'b', 'c'], ['a', 'a', 'c'], ['a', 'c', 'b'], ['a', 'b', 'b'], ['a', 'a', 'b'], ['a', 'c', 'a'], ['a', 'b', 'a'], ['a', 'a', 'a']]
and columns 2, 1, and 0 are descending (non-increasing) in that order,
just as you asked. What different were you expecting given the input.
and Manuel Garcia's solution and results: sortList = [(0,-1),(1,-1),(2,-1)]
.... Results: [['a', 'c', 'b'], ['a', 'c', 'a'], ['a', 'b', 'c'], ['a', 'b', 'b'], ['a', 'b', 'a'], ['a', 'a', 'c'], ['a', 'a', 'b'], ['a', 'a', 'a']]
Again, just as requested. Same question.
is this an issue with sort or is my code screwy?
Perhaps your understanding of ascending and descending? or of nested
sorting?
Terry J. Reedy
> Given that your example data all have 'a' in the first column, these statements of ill behavior make no sense!
Oh my! One would think that I just dashed off a question to the group
without spending any time on the problem (not the case). No excuses
for my oversight but thanks for your gentle reply :). This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Rachel Forder |
last post by:
Hi All,
I have a problem related to the sort function provided by STL.
class A{
A(string, string, int);
string itemA;
string itemB;
int...
|
by: Xah Lee |
last post by:
Sort a List
Xah Lee, 200510
In this page, we show how to sort a list in Python & Perl and also
discuss some math of sort.
To sort a list in...
|
by: Ed Sutton |
last post by:
I need to do a custom sort on a TreeView. I have various object types
associated with the TreeNode Tag property. I want to sort objects of
the...
|
by: TM |
last post by:
I am using an access database in my vb.net application and it is tied to a
datagrid.
My problem is that the field I want to sort on is a text...
|
by: Gene Hubert |
last post by:
I'm doing a custom sort in a datagrid. I'm overriding mousedown and
doing the sort on a hidden column in addition to the column that the
user...
|
by: Neil |
last post by:
Anyone know how to do a custom sort of a datagrid when a column header is
clicked?
Thanks
|
by: Emma Burrows |
last post by:
I have created a typed dataset in .Net 2.0 based on an Access database, and
set up various methods to retrieve specific data from the tables, etc...
|
by: Ethan Strauss |
last post by:
Hi,
I want to be able to create a custom sort order for a Sorted List.
Specifically, I have a grid which goes from A1 to H12. The default sort...
|
by: =?iso-8859-1?Q?=22Orlando_D=F6hring=22?= |
last post by:
Dear community,
I want to use the sort function to sort a (nested) list. General information can be found below.
...
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
|
by: jalbright99669 |
last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
|
by: Matthew3360 |
last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function.
Here is my code.
...
|
by: Arjunsri |
last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
|
by: WisdomUfot |
last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
|
by: Matthew3360 |
last post by:
Hi,
I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
|
by: Oralloy |
last post by:
Hello Folks,
I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA.
My problem (spelled failure) is with the...
|
by: BLUEPANDA |
last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
| |