简体   繁体   中英

how to sort an array of struct in ascending order in c++

I have an array in c++.

struct radius
{double r; double angle; double x; double y;};

radius LOCAL[1000];

I want to sort the LOCAL array according to the value r in ascending order. I have searched on the internet and most people suggested to use use sort() . But many use vector or non struct . (I don't want to use vector .)

std::sort takes two iterators, it's working for native arrays as well. All you need is to tell it how to compare two radius :

std::sort(std::begin(LOCAL), std::end(LOCAL), [](const radius &a, const radius &b){return a.r < b.r});
#include <cstdio>
#include <cstdlib>
#include <algorithm>

#define N 1000

struct radius {
    double r;
    double angle;
    double x;
    double y;
    static bool compOnR(const radius& a,const radius& b) { return a.r < b.r; }
};

int main(int argc, char* argv[] ) {
  radius LOCAL[N];
  std::srand(1);
  for (size_t i = 0; i < N; ++i)
      LOCAL[i].r = std::rand();
  std::sort(LOCAL,LOCAL+N,radius::compOnR);
  for (size_t i = 0; i < N; ++i)
      std::printf("%f\n",LOCAL[i].r);
  return 0;
}

g++ sort.cpp -o sort;
./sort;
## 79172.000000
## 2255680.000000
## 3823673.000000
## 5748438.000000
## 6364913.000000
## 9984370.000000
## 14238776.000000
## 15934104.000000
## 19815168.000000
## 21293906.000000
## 24780975.000000
## 25154455.000000
## 27170074.000000
## 28409913.000000
## 28707209.000000
## 36761397.000000
## 37649374.000000
## 38297258.000000
## 39987000.000000
## 46530724.000000
## 47314057.000000
## 47900753.000000
## 51113282.000000
## 53465943.000000
## 54348071.000000
## 54969874.000000
## 59527889.000000
## 60356493.000000
## 61699817.000000
## 65332131.000000
## 66110757.000000
## 68043415.000000
## 68706223.000000
## 71239541.000000
## 78408876.000000
## 81126604.000000
## 81410005.000000
## 81991855.000000
## 82953398.000000
## 89827071.000000
## 91507156.000000
## 95827548.000000
## 98508730.000000
## 104135668.000000
## 107488992.000000
## 109659376.000000
## 110751896.000000
## 113597342.000000
## 117920226.000000
## 120033497.000000
## 127483564.000000
## 128623131.000000
## 130019381.000000
## 131636656.000000
## 134707445.000000
## 140798883.000000
## 141553547.000000
## 142750431.000000
## 147699711.000000
## 148776516.000000
## 152201680.000000
## 154736414.000000
## 155562330.000000
## 163311491.000000
## 163503266.000000
## 164897092.000000
## 166723143.000000
## 167033398.000000
## 169580810.000000
## 170396422.000000
## 173227800.000000
## 175922353.000000
## 177169279.000000
## 178883767.000000
## 180281402.000000
## 182546393.000000
## 183411850.000000
## 185251411.000000
## 185252727.000000
## 185967244.000000
## 193202571.000000
## 196150922.000000
## 196796183.000000
## 199429704.000000
## 200171889.000000
## 203774338.000000
## 204255255.000000
## 208601227.000000
## 209050470.000000
## 209707626.000000
## 212567592.000000
## 212677188.000000
## 216482320.000000
## 217079209.000000
## 219218649.000000
## 220137366.000000
## 220256095.000000
## 223840042.000000
## 225550213.000000
## 232021905.000000
## 234179031.000000
## 236080650.000000
## 237123473.000000
## 239201037.000000
## 241418521.000000
## 246387401.000000
## 248294571.000000
## 248501142.000000
## 254320223.000000
## 257032043.000000
## 261159140.000000
## 261856647.000000
## 263875725.000000
## 271428884.000000
## 272769057.000000
## 277281651.000000
## 278464283.000000
## 278706088.000000
## 279314605.000000
## 288188008.000000
## 288380741.000000
## 288551719.000000
## 292425665.000000
## 293093510.000000
## 298088717.000000
## 302247655.000000
## 303305400.000000
## 305352816.000000
## 307819437.000000
## 308476873.000000
## 309610388.000000
## 310287807.000000
## 315469145.000000
## 318726364.000000
## 319628369.000000
## 325228391.000000
## 326665130.000000
## 328888169.000000
## 331017280.000000
## 331750578.000000
## 332167023.000000
## 333349106.000000
## 336758178.000000
## 338685731.000000
## 344555438.000000
## 347176135.000000
## 347371391.000000
## 348928353.000000
## 350621222.000000
## 351195142.000000
## 354043648.000000
## 360400841.000000
## 362645883.000000
## 365780715.000000
## 366619811.000000
## 367555595.000000
## 376727279.000000
## 381068804.000000
## 381320611.000000
## 382974263.000000
## 383555936.000000
## 384142618.000000
## 390271936.000000
## 391380553.000000
## 391458520.000000
## 395170739.000000
## 397147145.000000
## 398844030.000000
## 401155644.000000
## 401170801.000000
## 403305022.000000
## 403802322.000000
## 409247175.000000
## 411582458.000000
## 418344073.000000
## 432220402.000000
## 432507916.000000
## 432607168.000000
## 432751767.000000
## 439335255.000000
## 439953564.000000
## 442727410.000000
## 445349752.000000
## 447943845.000000
## 447992224.000000
## 450650277.000000
## 451230256.000000
## 452444896.000000
## 457063728.000000
## 458914655.000000
## 463352602.000000
## 464813391.000000
## 470441286.000000
## 470761036.000000
## 471898585.000000
## 475908256.000000
## 478762220.000000
## 478945608.000000
## 481376529.000000
## 481509805.000000
## 483314942.000000
## 483951940.000000
## 489353746.000000
## 494070682.000000
## 496677899.000000
## 498501259.000000
## 501708870.000000
## 501832017.000000
## 502141743.000000
## 503156218.000000
## 509813595.000000
## 509860471.000000
## 510083577.000000
## 514932214.000000
## 515016123.000000
## 517753386.000000
## 518225534.000000
## 518763463.000000
## 521884893.000000
## 524643866.000000
## 527534543.000000
## 527723017.000000
## 531178256.000000
## 531770466.000000
## 532705582.000000
## 533958746.000000
## 535702629.000000
## 537716674.000000
## 542938503.000000
## 548636271.000000
## 552577707.000000
## 553189268.000000
## 553475508.000000
## 553585300.000000
## 553909176.000000
## 554973902.000000
## 556727422.000000
## 557511759.000000
## 559325074.000000
## 560775615.000000
## 561035572.000000
## 562105037.000000
## 562984838.000000
## 563641187.000000
## 564230990.000000
## 564730604.000000
## 566314636.000000
## 566849098.000000
## 568066184.000000
## 578512110.000000
## 583332387.000000
## 584975152.000000
## 585293993.000000
## 587136518.000000
## 589473924.000000
## 589638738.000000
## 590092360.000000
## 593400968.000000
## 598476328.000000
## 600221024.000000
## 604083092.000000
## 604257737.000000
## 605540660.000000
## 606947925.000000
## 607161717.000000
## 612172294.000000
## 612835719.000000
## 614127119.000000
## 618869415.000000
## 619313288.000000
## 627711606.000000
## 629056069.000000
## 629266717.000000
## 630556269.000000
## 631623002.000000
## 634535219.000000
## 635231437.000000
## 643287356.000000
## 646953787.000000
## 649104225.000000
## 653528017.000000
## 653962273.000000
## 659169187.000000
## 659481682.000000
## 659998484.000000
## 665518468.000000
## 668882480.000000
## 669388451.000000
## 674043135.000000
## 678295046.000000
## 679620976.000000
## 681350654.000000
## 682476207.000000
## 682687163.000000
## 683337405.000000
## 685789554.000000
## 685894563.000000
## 686078705.000000
## 689179015.000000
## 691055053.000000
## 695475948.000000
## 696848150.000000
## 697552334.000000
## 699304830.000000
## 700130694.000000
## 704495905.000000
## 706140952.000000
## 710498872.000000
## 713161153.000000
## 713526954.000000
## 714283326.000000
## 714519849.000000
## 714777970.000000
## 715669847.000000
## 718559873.000000
## 718583407.000000
## 720130751.000000
## 720217822.000000
## 722318506.000000
## 725914710.000000
## 730103625.000000
## 730359853.000000
## 730417256.000000
## 732013956.000000
## 735206212.000000
## 738618398.000000
## 741274453.000000
## 745411997.000000
## 747030705.000000
## 747538802.000000
## 751486327.000000
## 756912098.000000
## 760329868.000000
## 760810055.000000
## 760913949.000000
## 761170564.000000
## 763144839.000000
## 763558938.000000
## 770640917.000000
## 772004055.000000
## 772315882.000000
## 773648064.000000
## 777206980.000000
## 791618821.000000
## 795420852.000000
## 797241922.000000
## 801013197.000000
## 801823640.000000
## 801874771.000000
## 801884112.000000
## 802799868.000000
## 803118292.000000
## 806457298.000000
## 806799588.000000
## 807062353.000000
## 807426509.000000
## 808103029.000000
## 808284658.000000
## 808450025.000000
## 810800351.000000
## 812669700.000000
## 812840796.000000
## 818041954.000000
## 819515399.000000
## 825207674.000000
## 827166045.000000
## 832433964.000000
## 832982510.000000
## 836056575.000000
## 842511577.000000
## 846594613.000000
## 849040635.000000
## 852188688.000000
## 853016194.000000
## 853477236.000000
## 853952711.000000
## 854366610.000000
## 855272970.000000
## 857575454.000000
## 858421365.000000
## 858703732.000000
## 860610480.000000
## 862720893.000000
## 866639895.000000
## 871902483.000000
## 873213988.000000
## 873783312.000000
## 875677531.000000
## 877082119.000000
## 880180058.000000
## 880268351.000000
## 884423791.000000
## 885118797.000000
## 886027271.000000
## 893065161.000000
## 893090906.000000
## 894080107.000000
## 901157396.000000
## 901252293.000000
## 903506249.000000
## 904629749.000000
## 905277879.000000
## 905284679.000000
## 913595887.000000
## 915035296.000000
## 916382919.000000
## 917548833.000000
## 921499854.000000
## 925804739.000000
## 927004625.000000
## 928126551.000000
## 931894842.000000
## 934901149.000000
## 938993624.000000
## 941104596.000000
## 941604920.000000
## 943538704.000000
## 944216783.000000
## 946355252.000000
## 947037966.000000
## 948801906.000000
## 951656719.000000
## 952331128.000000
## 954882794.000000
## 957482161.000000
## 959398704.000000
## 961603703.000000
## 963315143.000000
## 964776169.000000
## 967911703.000000
## 968295562.000000
## 970817048.000000
## 971464216.000000
## 972917810.000000
## 979193316.000000
## 983059344.000000
## 984872853.000000
## 988334858.000000
## 989502643.000000
## 990265717.000000
## 993687334.000000
## 994162635.000000
## 999426979.000000
## 1003224417.000000
## 1006108548.000000
## 1008690903.000000
## 1009134479.000000
## 1011048302.000000
## 1013063478.000000
## 1014983331.000000
## 1016194674.000000
## 1018269580.000000
## 1021817290.000000
## 1021923565.000000
## 1023689365.000000
## 1027737292.000000
## 1029201993.000000
## 1029708888.000000
## 1030506577.000000
## 1033148016.000000
## 1033522980.000000
## 1037007569.000000
## 1037430311.000000
## 1038804875.000000
## 1039201055.000000
## 1041127595.000000
## 1048555821.000000
## 1048973890.000000
## 1059749906.000000
## 1060528228.000000
## 1065189335.000000
## 1069560513.000000
## 1070258298.000000
## 1072439836.000000
## 1072603645.000000
## 1073113430.000000
## 1074369922.000000
## 1077596914.000000
## 1080478063.000000
## 1081471949.000000
## 1081763209.000000
## 1082024643.000000
## 1084695048.000000
## 1085377743.000000
## 1085983583.000000
## 1086024347.000000
## 1086979690.000000
## 1089957932.000000
## 1090543449.000000
## 1097051118.000000
## 1097888154.000000
## 1097905455.000000
## 1098551742.000000
## 1099044961.000000
## 1099874732.000000
## 1100017519.000000
## 1100747337.000000
## 1102345774.000000
## 1103580243.000000
## 1104530716.000000
## 1104667212.000000
## 1105271097.000000
## 1106023605.000000
## 1106757930.000000
## 1107735694.000000
## 1108087033.000000
## 1108323206.000000
## 1114316400.000000
## 1114506820.000000
## 1114827153.000000
## 1117104852.000000
## 1117157996.000000
## 1121797353.000000
## 1122491832.000000
## 1130014323.000000
## 1131959585.000000
## 1134381152.000000
## 1135550597.000000
## 1136350866.000000
## 1140807480.000000
## 1141343840.000000
## 1144638575.000000
## 1148026995.000000
## 1152143924.000000
## 1152299427.000000
## 1152334976.000000
## 1153542749.000000
## 1153725528.000000
## 1154003916.000000
## 1158588232.000000
## 1159789171.000000
## 1162346597.000000
## 1162436278.000000
## 1163821529.000000
## 1164186912.000000
## 1166822125.000000
## 1170075467.000000
## 1170490215.000000
## 1170970491.000000
## 1173748948.000000
## 1173969070.000000
## 1179202287.000000
## 1180219468.000000
## 1182095963.000000
## 1182559974.000000
## 1182909373.000000
## 1185694319.000000
## 1191391529.000000
## 1195355616.000000
## 1196032868.000000
## 1198427210.000000
## 1200824515.000000
## 1201886571.000000
## 1205023123.000000
## 1205254645.000000
## 1205536273.000000
## 1207248983.000000
## 1207817128.000000
## 1207988557.000000
## 1209526756.000000
## 1211082011.000000
## 1213508984.000000
## 1214951723.000000
## 1216685007.000000
## 1220226186.000000
## 1221544808.000000
## 1221792728.000000
## 1222814910.000000
## 1225161330.000000
## 1226454173.000000
## 1226846017.000000
## 1229521299.000000
## 1229915491.000000
## 1233326029.000000
## 1238258574.000000
## 1238744708.000000
## 1241424527.000000
## 1243681368.000000
## 1248402490.000000
## 1250486267.000000
## 1251951718.000000
## 1257083000.000000
## 1257188583.000000
## 1258014823.000000
## 1258046232.000000
## 1258227744.000000
## 1259406254.000000
## 1265491377.000000
## 1267998018.000000
## 1269950142.000000
## 1270216262.000000
## 1274341732.000000
## 1277092596.000000
## 1277164213.000000
## 1280937363.000000
## 1282858591.000000
## 1286148116.000000
## 1287159201.000000
## 1287487106.000000
## 1287531640.000000
## 1287998487.000000
## 1288617208.000000
## 1291092544.000000
## 1292149840.000000
## 1292597520.000000
## 1296635185.000000
## 1296877747.000000
## 1296887763.000000
## 1297330760.000000
## 1297990419.000000
## 1301950427.000000
## 1304108467.000000
## 1307241236.000000
## 1309435107.000000
## 1311361447.000000
## 1312083895.000000
## 1317442754.000000
## 1319159302.000000
## 1322111512.000000
## 1322220426.000000
## 1323822158.000000
## 1324920401.000000
## 1331020202.000000
## 1332884083.000000
## 1333033974.000000
## 1333654407.000000
## 1333824641.000000
## 1335928757.000000
## 1336030856.000000
## 1339228195.000000
## 1339394775.000000
## 1341788522.000000
## 1343191860.000000
## 1344082095.000000
## 1344887256.000000
## 1346491165.000000
## 1349677414.000000
## 1355198126.000000
## 1358225049.000000
## 1359827009.000000
## 1361784847.000000
## 1362431466.000000
## 1363674760.000000
## 1364873477.000000
## 1365019485.000000
## 1370429126.000000
## 1372563802.000000
## 1374726709.000000
## 1376731875.000000
## 1377520760.000000
## 1378954182.000000
## 1380886342.000000
## 1382647790.000000
## 1382741976.000000
## 1382875695.000000
## 1384404671.000000
## 1388644766.000000
## 1388646894.000000
## 1389234619.000000
## 1390979634.000000
## 1393479897.000000
## 1394623359.000000
## 1399374302.000000
## 1400427944.000000
## 1408884323.000000
## 1409718917.000000
## 1409982267.000000
## 1410215546.000000
## 1413917078.000000
## 1414036229.000000
## 1415516123.000000
## 1417872782.000000
## 1418544567.000000
## 1418587763.000000
## 1419256079.000000
## 1421877144.000000
## 1424365729.000000
## 1426542624.000000
## 1429623093.000000
## 1430549897.000000
## 1432859435.000000
## 1433509418.000000
## 1434133201.000000
## 1434168432.000000
## 1434491728.000000
## 1436430327.000000
## 1440931204.000000
## 1441680395.000000
## 1443945038.000000
## 1444887337.000000
## 1445357088.000000
## 1452500437.000000
## 1454008490.000000
## 1455244164.000000
## 1455658048.000000
## 1456590270.000000
## 1459473287.000000
## 1461651558.000000
## 1463785653.000000
## 1463846098.000000
## 1463890282.000000
## 1464321379.000000
## 1466378811.000000
## 1466668831.000000
## 1468555678.000000
## 1470551807.000000
## 1471641423.000000
## 1475831917.000000
## 1481765933.000000
## 1481858287.000000
## 1482338357.000000
## 1484742720.000000
## 1484908657.000000
## 1486937972.000000
## 1488226198.000000
## 1489252809.000000
## 1491323753.000000
## 1499751563.000000
## 1500744350.000000
## 1502577918.000000
## 1503213501.000000
## 1504212670.000000
## 1505099306.000000
## 1510014273.000000
## 1517512817.000000
## 1518954509.000000
## 1522010226.000000
## 1524649669.000000
## 1525440093.000000
## 1533584453.000000
## 1537501517.000000
## 1540636934.000000
## 1541238765.000000
## 1546965957.000000
## 1549232205.000000
## 1551482697.000000
## 1553515998.000000
## 1556587278.000000
## 1559931134.000000
## 1567506547.000000
## 1575147694.000000
## 1576460931.000000
## 1579011701.000000
## 1579233404.000000
## 1580643061.000000
## 1588783404.000000
## 1589403090.000000
## 1589759001.000000
## 1591926313.000000
## 1600558245.000000
## 1601537992.000000
## 1602729120.000000
## 1604321728.000000
## 1605816179.000000
## 1605919245.000000
## 1616740977.000000
## 1619990833.000000
## 1621461943.000000
## 1622912089.000000
## 1623022576.000000
## 1626471873.000000
## 1628532311.000000
## 1631816430.000000
## 1631817825.000000
## 1632646225.000000
## 1634036761.000000
## 1637793905.000000
## 1639712616.000000
## 1646230978.000000
## 1664583939.000000
## 1665564232.000000
## 1669566824.000000
## 1671189783.000000
## 1675026723.000000
## 1681643301.000000
## 1682827604.000000
## 1683200258.000000
## 1684357629.000000
## 1685673045.000000
## 1691968674.000000
## 1693904716.000000
## 1695848449.000000
## 1696058609.000000
## 1698868148.000000
## 1699274596.000000
## 1700375105.000000
## 1706906089.000000
## 1710624189.000000
## 1710691981.000000
## 1714001700.000000
## 1714404997.000000
## 1715681891.000000
## 1715848038.000000
## 1716688810.000000
## 1718458134.000000
## 1722668324.000000
## 1725640579.000000
## 1726212481.000000
## 1729267236.000000
## 1731415693.000000
## 1734163363.000000
## 1738262271.000000
## 1739141933.000000
## 1745745614.000000
## 1746795941.000000
## 1748618304.000000
## 1748704243.000000
## 1749675386.000000
## 1752628680.000000
## 1756377564.000000
## 1761818666.000000
## 1762321158.000000
## 1764841629.000000
## 1766726419.000000
## 1770079778.000000
## 1770433885.000000
## 1778927100.000000
## 1783003130.000000
## 1783671750.000000
## 1789630847.000000
## 1790613439.000000
## 1794437291.000000
## 1794593155.000000
## 1796613051.000000
## 1799204785.000000
## 1808026603.000000
## 1808733278.000000
## 1812158119.000000
## 1815458064.000000
## 1817376089.000000
## 1823976466.000000
## 1824415734.000000
## 1826087862.000000
## 1829649326.000000
## 1830678472.000000
## 1840531613.000000
## 1840579929.000000
## 1841827055.000000
## 1843638549.000000
## 1843654049.000000
## 1844934695.000000
## 1846160353.000000
## 1846206506.000000
## 1850951684.000000
## 1852483211.000000
## 1853102625.000000
## 1853580352.000000
## 1856224190.000000
## 1857719385.000000
## 1860379350.000000
## 1860692743.000000
## 1861396580.000000
## 1861506626.000000
## 1863334004.000000
## 1868830340.000000
## 1873226917.000000
## 1874846520.000000
## 1875221517.000000
## 1875989888.000000
## 1876268425.000000
## 1877548652.000000
## 1877819183.000000
## 1880675250.000000
## 1881628697.000000
## 1882688114.000000
## 1883488164.000000
## 1883570151.000000
## 1884096778.000000
## 1885352671.000000
## 1885642676.000000
## 1886071792.000000
## 1888581007.000000
## 1889772843.000000
## 1890522095.000000
## 1891777678.000000
## 1895591035.000000
## 1899333077.000000
## 1899971110.000000
## 1900106306.000000
## 1900413059.000000
## 1906580382.000000
## 1908911711.000000
## 1910500675.000000
## 1917129097.000000
## 1917509526.000000
## 1917831363.000000
## 1920301185.000000
## 1923373702.000000
## 1928854726.000000
## 1930358829.000000
## 1931858668.000000
## 1934278859.000000
## 1941574088.000000
## 1942525379.000000
## 1947260757.000000
## 1948283602.000000
## 1949118330.000000
## 1952339209.000000
## 1957115006.000000
## 1961969356.000000
## 1963071900.000000
## 1965117085.000000
## 1966331975.000000
## 1968925557.000000
## 1970631261.000000
## 1971002845.000000
## 1971404448.000000
## 1974817238.000000
## 1976151787.000000
## 1977056430.000000
## 1979576219.000000
## 1979932169.000000
## 1985327809.000000
## 1987818732.000000
## 1988875958.000000
## 1989349348.000000
## 1992516190.000000
## 1994411750.000000
## 1994719310.000000
## 1996606817.000000
## 1999845055.000000
## 2002183737.000000
## 2002583103.000000
## 2004909133.000000
## 2006807452.000000
## 2011243547.000000
## 2011691067.000000
## 2014059754.000000
## 2016619802.000000
## 2017170047.000000
## 2018090483.000000
## 2019386625.000000
## 2021467389.000000
## 2023351876.000000
## 2024505183.000000
## 2028664838.000000
## 2033644404.000000
## 2034318386.000000
## 2035222245.000000
## 2038269862.000000
## 2038742564.000000
## 2039294279.000000
## 2040848174.000000
## 2041654184.000000
## 2045292424.000000
## 2047965620.000000
## 2051148034.000000
## 2052516265.000000
## 2052770292.000000
## 2054150484.000000
## 2058976528.000000
## 2059320408.000000
## 2060717375.000000
## 2062300692.000000
## 2064206049.000000
## 2064599183.000000
## 2064630432.000000
## 2069834510.000000
## 2070386607.000000
## 2070592570.000000
## 2071339238.000000
## 2072861359.000000
## 2073836549.000000
## 2074025239.000000
## 2078178978.000000
## 2080815710.000000
## 2084670932.000000
## 2085406596.000000
## 2086000814.000000
## 2090651077.000000
## 2093441090.000000
## 2094585459.000000
## 2096129536.000000
## 2097749304.000000
## 2098119049.000000
## 2098560397.000000
## 2102803426.000000
## 2103210887.000000
## 2104285884.000000
## 2105754108.000000
## 2105910311.000000
## 2109934474.000000
## 2110483414.000000
## 2110844165.000000
## 2112026358.000000
## 2114797047.000000
## 2117009139.000000
## 2118275762.000000
## 2119826911.000000
## 2121455785.000000
## 2127991775.000000
## 2129482318.000000
## 2129671546.000000
## 2133968316.000000
## 2138045481.000000
## 2138392511.000000
## 2139193777.000000
## 2140806731.000000
## 2145222948.000000
## 2145324179.000000

When using sort(), you can either provide a comparison function as third parameter, or you can define the < (less than) operator for the struct:

struct radius {
   double r; double angle; double x; double y;
   bool operator < (const radius& another) const {
       return r < another.r;
   }
};

Then you can simply call:

sort(std::begin(LOCAL), std::end(LOCAL));

By defining the less-than operator, your struct can also be put into set .

The qsort function does what you want. You can also use std::sort in C++

int radius_compar(const void * a1, const void* a2)
{
    radius* r1 = (radius*)a1;
    radius* r2 = (radius*)a2;

    // TODO: Implement proper floating point comparison
    return r1->r < r2->r ? -1 :
             r1->r > r2->r ? 1 :
             0;
}

qsort( LOCAL, sizeof(LOCAL)/sizeof(LOCAL[0]), sizeof(radius), radius_compar );

Importantly, check out this article about some details on floating point comparison.

Here's how to do it with std::sort:

bool RadiusLessThan( const radius& r1, const radius& r2 )
{
    // Note that this is not a safe way to compare floats
    // it's just for illustration.
    return r1->r < r1->r;
}

std::sort( LOCAL, LOCAL+sizeof(LOCAL)/sizeof(LOCAL[0]), RadiusLessThan );

You can use qsort() function.

#include <iostream>
#include <cstdlib>

const int LOCAL_SIZE = 10;

struct radius {
    double r; double angle; double x; double y;
};

int ascending(const void *d1, const void *d2);

int main(void) {
    radius LOCAL[LOCAL_SIZE] = {
        { -1, },
        { 0, },
        { 7, },
        { 2, },
        { 6, },
        { 5, },
        { 8, },
        { 9.9, },
        { 138, },
        { -1666 }
    };
    qsort(LOCAL, LOCAL_SIZE, sizeof(radius), ascending);

    for (radius &d : LOCAL) {
        std::cout << d.r << std::endl;
    }

    return 0;
}

int ascending(const void *pd1, const void *pd2) {
    const radius *r1 = (const radius *)pd1, *r2 = (const radius *)pd2;
    if (r1->r > r2->r)
    {
        return 1;
    }
    else if (r1->r < r2->r)
    {
        return -1;
    }
    else
    {
        return 0;
    }
}

Besides the answers already provided, in your structure definition, you can override the '<' operator too -

radius::operator<(const radius &y)
{
    if(this->r < y.r)
         return true;
    else
         return false;   
}

and This should make the sorting function work just fine. I am assuming that you won't be needing a different comparison function on radius structure.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM