Question on sorting

Lad export at hope.cz
Wed Dec 1 11:24:26 EST 2004


Peter Otten <__peter__ at web.de> wrote in message news:<cokacu$fp6$04$1 at news.t-online.com>...
> Lad wrote:
> 
> > wes weston <wweston at att.net> wrote in message
> > news:<0gKqd.72252$7i4.43429 at bgtnsc05-news.ops.worldnet.att.net>...
> >> Lad wrote:
> >> > Hi,
> >> > I have a file of records of 4 fields each.
> >> > Each field is separated by a semicolon. That is
> >> > 
> >> > Filed1;Ffield2;Field3;Field4
> >> > 
> >> > But there may be also empty records such as
> >> > ;;;;
> >> > (only semicolons).
> >> > 
> >> > For sorting I used
> >> > #################
> >> > lines = file('Config.txt').readlines()# a file I want to sort
> >> > lines.sort()
> >> > ff=open('ConfigSorted.txt','w')# sorted file
> >> > ff.writelines(lines)
> >> > ff.close()
> >> > ###############
> >> > It was sorted but empty records were first. I need them to be last(at
> >> > the end of the file). How can I do that?
> >> > 
> >> > Thanks for help
> >> > Lad
> >> 
> >> Lad,
> >>     The sort call can have a function name as an arg. You
> >> could do:
> >> 
> >> def mycompare(s1,s2):
> >> #return -1 to put s1's at front; 1 to put s1's at back; 0 for a tie
> >> #if s1==";;;;" and s2<>";;;;": return 1
> >> 
> >> lines.sort(mycompare)
> >> 
> > Wes,
> > Thank you for reply. But I do not understand mycompare function. Can
> > you please explain to me how it should work? Thanks
> 
> compare(a, b) is just a function for comparing two items/lines. I must
> return -1 if a<b, +1 if a>b, and 0 if a==b. For example the following
> compare moves the ";;;;" records to the end and keeps the order of others
> unaffected:
> 
> >>> items = [";;;;", ";a;b;;", ";b;a;;", "a;b;c;d;e", "a;;;d;e"]
> >>> def compare(a, b):
> ...     return cmp(a == ";;;;", b == ";;;;") or cmp(a, b)
> ...
> >>> items.sort(compare)
> >>> items
> [';a;b;;', ';b;a;;', 'a;;;d;e', 'a;b;c;d;e', ';;;;']
> 
Petr,
thank you for help and explanation.
It works
Lad



More information about the Python-list mailing list