Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9236695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:21:30+00:00 2026-06-18T07:21:30+00:00

i need to sort the first column of a table. it looks something like

  • 0

i need to sort the first column of a table. it looks something like

6000 799 
7000 352
8000 345
9000 234
10000 45536 
11000 3436
1000 342
2000 123
3000 1235
4000 234
5000 233

i want the first column to be in ascending order, but it is sorting it by only the first digit, not the value of the whole column, i.e.

1000 342
10000 45536
11000 3436
2000 123

But i want

1000 342
2000 123
3000 1235
etc

Currently trying:

SortInputfile=open("InterpBerg1","r")
line=SortInputfile.readlines()
line.sort()
map(SortOutputfile.write, line)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T07:21:32+00:00Added an answer on June 18, 2026 at 7:21 am

    The sort and sorted function support a key argument which allows you to specify the key which should be used to perform the sorting. Since you want a numerical sort order and no alphabetical sort order you need to extract the first column and convert it to an int:

    SortInputfile=open("InterpBerg1","r")
    line=SortInputfile.readlines()
    line.sort(key=lambda line: int(line.split()[0]))
    map(SortOutputfile.write, line)
    

    A cleaner version of this could would be:

    # read input file
    with open(input_filename) as fh:
        lines = fh.readlines()
    
    # sort lines
    lines.sort(key=lambda line: int(line.split()[0]))
    
    # write output file
    with open(output_filename, 'w') as fh:
        fh.writelines(lines)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to sort a flat file by third column leaving first column intact
I need to Sort the first column of the Job matrix and when giving
I have this array and I need to sort it, first by key_1 and
I need to sort an array that can look like this: $array[4][0] = array('id'
My table has two columns which I need to sort by: content title Only
I have a general query that looks like this: SELECT DISTINCT pb.id, pb.last, pb.first,
Two columns town and priority. I need to sort table, so that towns with
I'm looking for solution which gives ability to disable column sort in jqGrid table
I need to sort list of strings in the alphabetical order: List<String> list =
I need to sort nodes in xml. I have the following code which successfully

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.