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

  • Home
  • SEARCH
  • 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 9135275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:48:10+00:00 2026-06-17T08:48:10+00:00

I have a question about sorting data by multiple columns. I’m definitely a beginner

  • 0

I have a question about sorting data by multiple columns. I’m definitely a beginner at this and am wondering how I can sort by one column and then by another without losing the ordering of the first column. I have a file of tab separated data consisting of three columns. The majority of the data isn’t paired (one id, first column, and position start and end, second and third columns). Occasionally, however, there are multiple entries for the same ID (first column). These need to remain grouped together (without a space separating them from the next entry, unless it has a different ID). The data is really already sorted with respect to the first column, but I need to sort it numerically based on the starting position (second column) while preserving the original sorting. Like this:

Current format:

PITG_00129  606 1436

PITG_00130  1   987

PITG_00132  2   1321

PITG_00133 4464 11708
PITG_00133 1 2946
PITG_00133 4081 4515

Desired format:

PITG_00129  606 1436

PITG_00130  1   987

PITG_00132  2   1321

PITG_00133 1 2946
PITG_00133 4081 4515
PITG_00133 4464 11708
  • 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-17T08:48:11+00:00Added an answer on June 17, 2026 at 8:48 am

    You can do this pretty easily in python. First, you need to read your data in a proper format:

    def line_to_tuple(line):
        data = line.split()
        return (data[0],int(data[1]),int(data[2]))
    

    This will turn each line into a tuple which will sort lexicographically. Since your strings (the first column) are set up in an easily sorted manner, we don’t need to worry about them. The second and third columns just need to be converted to integers to make them sort properly.

    with open(inputfile) as fin, open(outputfile,'w') as fout:
        non_blank_lines = (line for line in fin if line.strip())
        sorted_lines = sorted(non_blank_lines,key=line_to_tuple)
        fout.writelines(sorted_lines)
    

    Here’s another implementation to preserve blank lines between fields:

    import itertools
    def field1(line):
        data = line.split()
        try:
            return data[0]
        except IndexError:
            return None
    
    def fields(line):
        data = line.split()
        return data[0],int(data[1]),int(data[2])
    
    with open('test.dat') as fin, open('output.dat','w') as fout:
        for k,v in itertools.groupby(fin,key=field1):
            if k is None:
                fout.write('\n')
            else:
                fout.writelines(sorted(v,key=fields))
    

    This uses itertools to chunk up the file based on the empty lines and sorts those groups individually before writing them back out.

    Here’s the output:

    temp $ cat output.dat 
    PITG_00129  606 1436
    
    PITG_00130  1   987
    
    PITG_00132  2   1321
    
    PITG_00133 1 2946
    PITG_00133 4081 4515
    PITG_00133 4464 11708
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a question about sorting in Yii DataProvider. I want to sort it
I have a question about sorting. int[][] array = {{4,2},{1,7},{4,5},{1,2},{1,1},{4,1}}; After sorting this array,
Question about GridView sorting in VB.NET: I have a GridView with AutoGenerateColumns = True
I have question about interpreting strings as packed binary data in C++. In python,
i have question about YAJLiOS parser... I have next json data : {{ body
I have a question about this formula from a book: EFW (cm,kg)= 10^(-1,7492+(0,166*BPD)+(0,046*AC)-(2,646*AC*BPD/1000)) The
I am studying Java and sorting. I have a question about tracking the index
So, this question may be a little vague, but I have constant discussions about
I am studying JAVA and I have a question about Sorting string. In the
My question is about how to sort an ArrayList with custom Object by one

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.