Message Boards Message Boards

1
|
3618 Views
|
3 Replies
|
6 Total Likes
View groups...
Share
Share this post:

Sorting a list of astronomical objects by distance from Earth

Posted 2 years ago

I have a list of astronomical objects that I need to sort by distance from Earth:

{Alpha Centauri, Andromeda Galaxy, Antennae Galaxy, Eagle Nebula, Jupiter, Moon, Orion Nebula, Pleiades Star Cluster, Pluto, Ring Nebula, Sombrero Galaxy, Sun}

Entering "distance {list}" returns a table of distances in different units, and using the Sort and SortBy functions with distance will only interpret one or two items from the list. Is there a way to sort these distances, or at least force all the distances to the same unit?

POSTED BY: Owen Lieberman
3 Replies
Posted 2 years ago

Just realized that you were providing that input to Wolfram|Alpha, not Wolfram Language. I don't know how to sort and convert units in Wolfram|Alpha. Copy the Wolfram Language input from Wolfram|Alpha and wrap it in Hold.

entities = 
  Hold[{Entity["Star", "RigelKentaurusA"][EntityProperty["Star", "DistanceFromEarth"]], 
    Entity["Galaxy", "M31"][EntityProperty["Galaxy", "DistanceFromEarth"]], 
    EntityClass["Galaxy", "ARP244"][EntityProperty["Galaxy", "DistanceFromEarth"]], 
    Entity["StarCluster", "NGC6611"][EntityProperty["StarCluster", "DistanceFromEarth"]], 
    Entity["Planet", "Jupiter"][EntityProperty["Planet", "DistanceFromEarth"]], 
    Entity["PlanetaryMoon", "Moon"][EntityProperty["PlanetaryMoon", "DistanceFromEarth"]], 
    Entity["Nebula", "M42"][EntityProperty["Nebula", "DistanceFromEarth"]], 
    Entity["StarCluster", "M45"][EntityProperty["StarCluster", "DistanceFromEarth"]], 
    Entity["MinorPlanet", "Pluto"][EntityProperty["MinorPlanet", "DistanceFromEarth"]], 
    Entity["Nebula", "M57"][EntityProperty["Nebula", "DistanceFromEarth"]], 
    Entity["Galaxy", "M104"][EntityProperty["Galaxy", "DistanceFromEarth"]], 
    Entity["Star", "Sun"][EntityProperty["Star", "DistanceFromEarth"]]}];

names = entities /. (Entity | EntityClass)[_, e_][___] :> e // ReleaseHold
distances = entities // ReleaseHold // Flatten // DeleteMissing

sorted = AssociationThread[names, distances] // Sort

Convert to whatever distance unit you want e.g.

sorted // UnitConvert[#, "LightYears"] &
POSTED BY: Rohit Namjoshi

Yes it would really help to see the code for people to help. But having been there this is probably what you might need to convert all numbers to the same unit and then sort: https://reference.wolfram.com/language/ref/UnitConvert.html.en?source=footer

That’s one of my favorite aspect of Wolfram Language the unit conversions conveniences.

POSTED BY: Jack I Houng

Welcome to Wolfram Community!
Please make sure you know the rules: https://wolfr.am/READ-1ST
Please provide formatted sample code that demonstrates the problem and your trials. This will make it easier for other members to help you.
The rules above explain several methods to include your code.

POSTED BY: Moderation Team
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract