# What is the closest star to Earth other than the sun?

How to find the Sun's nearest neighbor star using Mathematica? I tried

but it didn't work

• Should this be reported as a bug? – Grayscale Jul 31 '17 at 14:12

a=Most@Sort[StarData[
EntityClass["Star", "StarNearest10"], {"Name",
"DistanceFromSun"}], #1[[2]] < #2[[2]] &]

(*{{"Proxima Centauri",   Quantity[4.2181,  "LightYears"]},
{"Rigel Kentaurus A",  Quantity[4.38982, "LightYears"]},
{"Rigel Kentaurus B",  Quantity[4.4001,  "LightYears"]},
{"Barnard's Star",     Quantity[5.9339,  "LightYears"]},
{"Wolf 359",           Quantity[7.78813, "LightYears"]},
{"Lalande 21185",      Quantity[8.30217, "LightYears"]},
{"Luyten 726-8 B",     Quantity[8.5573,  "LightYears"]},
{"Luyten 726-8 A",     Quantity[8.5573,  "LightYears"]},
{"Sirius",             Quantity[8.59093, "LightYears"]}*)

TimelinePlot[
Association[(a[[#, 1]] -> a[[#, 2]]) & /@ Range@Length@a],
DateFunction -> (DatePlus[DateObject[Round[#*365*24*60*60, 1]],
Quantity[-1900, "year"]] &), FrameLabel -> "Light Years",
PlotLabel -> "Distance from Sun"]


• Nice alternative use of TimelinePlot! – 2012rcampion Mar 21 '16 at 3:51
• Or using SortBy: a = Rest@SortBy[StarData[EntityClass["Star", "StarNearest10"], {"Name", "DistanceFromSun"}], Last] – Bob Hanlon Mar 21 '16 at 4:09
• Clever use of DateFunction. Is this the best idiom to make a timeline-like graphic whose axis is not time? – ConvexMartian Mar 26 '16 at 13:49
• AstronomicalData["StarNearest10"] – vito Sep 28 '16 at 9:01

To clarify why it is that your command did not work, here is what your free-form input was translated to:

EntityValue[EntityClass["Star", {"DistanceFromSun" -> TakeSmallest[1]}], "DistanceFromSun"]


This returns {Missing["NotAvailable"]} because the Sun itself is part of the "Star" domain and though it is closest to itself it (apparently) has no value for "DistanceFromSun". Note that this appears to be a bad job on the part of the free-form interpreter... the command closer to what I imagine you are interested in would be this:

EntityValue[EntityClass["Star", {"DistanceFromSun" -> TakeSmallest[1]}], "Name"]


in which case we get the output of {Sun} (which, though it is not the answer you are looking for, at least the problem would have been clearer).

Now to solve this problem, use TakeSmallest[2] instead of TakeSmallest[1] and look for the property "Name" instead of "DistanceFromSun" in EntityValue:

EntityValue[EntityClass["Star", {"DistanceFromSun" -> TakeSmallest[2]}], "Name"]


in which case we get the expected output of {Sun, Proxima Centauri}. I imagine though that you would probably only want the second element of this list, so you could do

EntityValue[EntityClass["Star", {"DistanceFromSun" -> TakeSmallest[2]}], "Name"][[2]]


and get the single output Proxima Centauri, as desired.

If you also want the distance to Proxima Centauri, you can do the following:

EntityValue[EntityClass["Star", {"DistanceFromSun" -> TakeSmallest[2]}], {"Name", "DistanceFromSun"}]


and you will get this output, as expected:

{{Sun, Missing["NotAvailable"]}, {Proxima Centauri, 4.2181 ly}}


Note that, as I mentioned, the "DistanceFromSun" for the Sun itself is Missing["NotAvailable"] and this is the output you originally received.