473,587 Members | 2,412 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question on sorting

Lad
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.tx t').readlines() # a file I want to sort
lines.sort()
ff=open('Config Sorted.txt','w' )# sorted file
ff.writelines(l ines)
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
Jul 18 '05 #1
5 1407
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.tx t').readlines() # a file I want to sort
lines.sort()
ff=open('Config Sorted.txt','w' )# sorted file
ff.writelines(l ines)
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(myco mpare)

wes

Jul 18 '05 #2
wes weston <ww*****@att.ne t> writes:
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.tx t').readlines() # a file I want to sort
lines.sort()
ff=open('Config Sorted.txt','w' )# sorted file
ff.writelines(l ines)
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(myc ompare)


I can't help feeling that the OP might have really wanted to be sorting on
individual fields rather than whole lines. In which case I would think of
doing a line.split(';') on each line before sorting. It would still need
either to use a function to make empty fields go later or alternatively use
DSU (google!) and convert '' to say '~' and back again. This also solves the
problem of what to expect when only some of the fields are blank rather than
all of them.

Eddie

Jul 18 '05 #3
Lad
wes weston <ww*****@att.ne t> wrote in message news:<0g******* ************@bg tnsc05-news.ops.worldn et.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.tx t').readlines() # a file I want to sort
lines.sort()
ff=open('Config Sorted.txt','w' )# sorted file
ff.writelines(l ines)
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(myco mpare)

Wes,
Thank you for reply. But I do not understand mycompare function. Can
you please explain to me how it should work? Thanks

LAd
Jul 18 '05 #4
Lad wrote:
wes weston <ww*****@att.ne t> wrote in message
news:<0g******* ************@bg tnsc05-news.ops.worldn et.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.tx t').readlines() # a file I want to sort
> lines.sort()
> ff=open('Config Sorted.txt','w' )# sorted file
> ff.writelines(l ines)
> 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(myco mpare)

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(comp are)
items [';a;b;;', ';b;a;;', 'a;;;d;e', 'a;b;c;d;e', ';;;;']

As Eddie Corns pointed out, you left some doubt whether that is really what
you want. Here is a more complex compare() that handles the lines as
columns split by ";" and puts empty columns last in the sorting order:
def key(row): .... return [(not col, col) for col in row.split(";")]
.... def compare(a, b): .... return cmp(key(a), key(b))
.... items.sort(comp are)
items ['a;b;c;d;e', 'a;;;d;e', ';a;b;;', ';b;a;;', ';;;;']

If you are on Python 2.4, you don't need the compare() detour and can use
key() directly:
items.sort(key= key)
items ['a;b;c;d;e', 'a;;;d;e', ';a;b;;', ';b;a;;', ';;;;']

Finally, the ";;;;" lines don't seem to carry any information - why not
filter them out completely?
items = [line[:-1] for line in file("cfg.txt", "U") if line != ";;;;\n"]

Peter

Jul 18 '05 #5
Lad
Peter Otten <__*******@web. de> wrote in message news:<co******* ******@news.t-online.com>...
Lad wrote:
wes weston <ww*****@att.ne t> wrote in message
news:<0g******* ************@bg tnsc05-news.ops.worldn et.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.tx t').readlines() # a file I want to sort
> lines.sort()
> ff=open('Config Sorted.txt','w' )# sorted file
> ff.writelines(l ines)
> 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(myco mpare)

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(comp are)
items

[';a;b;;', ';b;a;;', 'a;;;d;e', 'a;b;c;d;e', ';;;;']

Petr,
thank you for help and explanation.
It works
Lad
Jul 18 '05 #6

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

Similar topics

4
2526
by: dont bother | last post by:
This is really driving me crazy. I have a dictionary feature_vectors{}. I try to sort its keys using #apply sorting on feature_vectors sorted_feature_vector=feature_vectors.keys() sorted_feature_vector.sort() #feature_vector.keys()=sorted_feature_vector
7
2911
by: BRIAN | last post by:
I have been trying to use a couple of javascripts to sort a table by clicking on column headings. Sorttable.js and tablesort.js which I found on the web... I am encountering one problem. In my table, the content of the first cell in each row contains a link to another location. When my table is sorted the displayed table appears...
1
1590
by: Sen-Lung Chen | last post by:
Dear All: I have question about how to compile those below code. The architecture is as below: Independent: caluating.cc , reading.cc, ran_gen.cc, hamming.cc, sorting.cc, combine.cc ----------- Dependency: find_column.cc self + hamming.cc, sorting.cc
1
1180
by: PiPOW | last post by:
Hi. I have a question. I am developing an application and I have to choose the most suitable control to show records from some database queries, with freedom in customizing control layout and behavior, leaving data read-only. Good examples might be: control showing message list in Microsoft Outlook (or Outlook Express); download-in-progress...
21
3191
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each column. Once the array elements are split, what is the best way to sort them? Thank you. //populate data object with data from xml file. //Data is a...
1
1518
by: richard | last post by:
OK, I have got bi-directional datagrid sorting down, have a created a nice sorting class. My question, how can I add sorting to more than one datagrid on the same page? Everything I have tried has failed. Any ideas?
26
2567
by: mike-yue | last post by:
The topic comes from a question: Would you rather wait for the results of a quicksort, a linear search, or a bubble sort on a 200000 element array? 1Quicksort 2Linear Search 3Bubble Sort The answer is 2Linear Search
80
2397
by: Boltar | last post by:
Hi I need to store a number of integer values which I will then search on later to see if they exist in my container. Can someone tell me which container would be quickest for finding these values? I can't use a plain C array (unless I make it 2^32 in size!) since I don't know the max integer value. Thanks for any help
5
4922
by: jrod11 | last post by:
hi, I found a jquery html table sorting code i have implemented. I am trying to figure out how to edit how many colums there are, but every time i remove code that I think controls how many colums there are, it crashes. There are currently 6 columns, and I only want 4. How do I remove the last two (discount and date)? Here is a link:...
0
7918
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...
1
7967
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...
0
8220
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5392
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...
0
3840
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3875
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2353
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1185
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...

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.