Hello all,

I've searched a lot and found a bunch of answers and edited my comparison many many times, but just cannot get a sorted output.

Here is my struct

typedef struct sortedDistance {
    int storeID;
    int zipCode;
    double latitude;
    double longitude;
    double distance;
    char address[50];
    char city[50];
    char state[20];
} sortedDistance;

I want to sort by "double distance"

Here is my qsort() function call

qsort( sortedArray, sizeSortedDistanceArray, sizeof( sortedDistance ), compare );

And finally, here is my compare function

int compare( const void* a, const void* b ) {
    const sortedDistance *ia = (const sortedDistance *) a;
    const sortedDistance *ib = (const sortedDistance *) b;

    if( ia->distance < ib->distance ) return -1;
    if( ia->distance > ib->distance ) return 1;
    else return 0;
}

I've tried many different things, and I just cannot get it to sort by distance element of the struct array. Any assistance would be helpful!

Recommended Answers

All 2 Replies

Here, you can use a struct with integers from a file, and sort them. First, it takes them as a string, converts them to integers, builds an integer array, then uses a vector to sort ascending.

#include <string>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <sstream>

using namespace std;

struct location {
    string distances;
};

struct locations 
{
    string dist;
    struct sort_by_dist
    {
        bool operator ()(const locations& e1, const locations& e2) const
        {
            return (e1.dist > e2.dist);
        }
    };
};


int Distance()
{
    int add = 0;
    int num = 0;
    location database;
    string fLine;

    int pcount = 0;
    int store[14] = { 0 }; 
    ifstream distance;
    distance.open("distance.txt");

    while (!distance.eof())
    {
    getline(distance, fLine);
    database.distances = fLine;
    stringstream ss;
    string str = database.distances; 
    int num = stoi(str);
    ss << str;
    std::cout << num << " "; //interger form
  //  std::cout << database.distances << std::endl; //string form

           store[add] = num;
           pcount++;
           add++;
    } 

    distance.close();
    vector<int> numbers;
    sort(store, store + pcount);

    std::cout << "\n";
    cout << "Sorted \n";
    for (auto x : store)
        cout << x << " ";


    std::cout << "" << std::endl;
    return 0;
}

void find_oldest(locations array[], size_t n)
{
    std::sort(array, array + n,
        [](const locations& e1, const locations& e2)
        {
            return (e1.dist > e2.dist);
        });

    for (size_t i = 0; i < n; i++){

        }
}


int main()
{
    Distance();
    system("pause");
}

150 15 115 50 200 100 25 45 35 15 50 20 30 25
Sorted
15 15 20 25 25 30 35 45 50 50 100 115 150 200

distance.txt
150
15
115
50
200
100
25
45
35
15
50
20
30
25

OP - I don't see a problem with your code. I would hope by now you found it to work.

Here's a pared down version of your problem, and it works fine for me.

#include <stdio.h>
#include <stdlib.h>

typedef struct sortedDistance {

    int num;
    double distance;

} sortedDistance;

int compare( const void* a, const void* b ) {
    const sortedDistance *ia = (const sortedDistance *) a;
    const sortedDistance *ib = (const sortedDistance *) b;

    if( ia->distance < ib->distance ) return -1;
    if( ia->distance > ib->distance ) return 1;
    else return 0;
}
int main()
{
    sortedDistance a[5];
    int i;

    //let's make some data
    for (i = 0; i < 5; i++ )
    {
        a[i].distance = (double)(rand( ) % 50);
        a[i].num = i;
    }

    //original order
    for (i = 0; i < 5; i++ )
        printf( "%lf ", a[i].distance );

    printf( "\n");

    qsort( a, 5, sizeof( sortedDistance ), compare );

    //now see that it's sorted.
    for (i = 0; i < 5; i++ )
        printf( "%d %.0lf ", a[i].num, a[i].distance );

    printf( "\n");

    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.