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 9146945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:52:08+00:00 2026-06-17T10:52:08+00:00

For example, to sort an array of complex numbers first by real part, then

  • 0

For example, to sort an array of complex numbers first by real part, then by magnitude of imaginary part, and then with negative imaginary parts first:

def order(a):
    return a.real, abs(a.imag), sign(a.imag)

z = array(sorted(z, key=order))

So

array([ 1.+2.j, 5.+0.j, 1.+0.j, 1.+1.j, 1.+1.j, 1.-1.j, 6.+0.j, 1.-1.j, 1.-2.j])

becomes

array([ 1.+0.j, 1.-1.j, 1.-1.j, 1.+1.j, 1.+1.j, 1.-2.j, 1.+2.j, 5.+0.j, 6.+0.j])

I think there’s a way to do the same thing using numpy’s argsort, which is probably faster, but I can’t figure it out:

In [2]: argsort((a.real, abs(a.imag), sign(a.imag)))
Out[2]: 
array([[0, 2, 3, 4, 5, 7, 8, 1, 6],
       [1, 2, 6, 3, 4, 5, 7, 0, 8],
       [5, 7, 8, 1, 2, 6, 0, 3, 4]])
  • 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-17T10:52:09+00:00Added an answer on June 17, 2026 at 10:52 am

    You can use np.lexsort :

    import numpy as np
    a = np.array([ 1.+2.j, 5.+0.j, 1.+0.j, 1.+1.j, 1.+1.j, 1.-1.j, 6.+0.j,
                   1.-1.j, 1.-2.j])
    sorted_idx = np.lexsort((np.sign(a.imag), np.abs(a.imag), a.real))
    >>> a[sorted_idx]
    array([ 1.+0.j,  1.-1.j,  1.-1.j,  1.+1.j,  1.+1.j,  1.-2.j,  1.+2.j,
            5.+0.j,  6.+0.j])
    

    Notice that the sorting keys are in reversed ordered, i.e. last is principal.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

example some array {2,8,9,10,21,32,1,6,3...} first child take (data size / 2) and sort second
I would like to sort an array by increasing order of frequency. For example,
For example: int A[] = {3,2,1,2,3,2,1,3,1,2,3}; How to sort this array efficiently? This is
I have an example class containing two data points: public enum Sort { First,
I want to sort string array based on on input string , for example
I would like to sort an array in ascending order using C/C++ . The
I am implementing the complex algorithm which part is sorting array of ordered sequences
How can I sort an array based on a key's value occurrence and then
How do I sort an array with the highest points? Example: $sale = array();
I am trying to sort my data in the array list. For example the

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.