Unknown2007-08-28 01:31:28
This isn't in the mechanics corner, because it's got nothing to do with programming a Lusternia system...though I wish it did.
I'm currently working on a program that deals with parallel arrays, and than it itself is no big deal. But the program needs to be able to sort the data in these four arrays by four different criteria, as specified by the user. I've gotten everything down, except for the actual sorting, namely, the sorting of one of the arrays alphabetically.
Any advice on how to sort an array in such a way would be greatly appreciated.
I'm currently working on a program that deals with parallel arrays, and than it itself is no big deal. But the program needs to be able to sort the data in these four arrays by four different criteria, as specified by the user. I've gotten everything down, except for the actual sorting, namely, the sorting of one of the arrays alphabetically.
Any advice on how to sort an array in such a way would be greatly appreciated.
Saran2007-08-28 03:30:54
QUOTE(Deschain @ Aug 28 2007, 11:31 AM) 436553
This isn't in the mechanics corner, because it's got nothing to do with programming a Lusternia system...though I wish it did.
I'm currently working on a program that deals with parallel arrays, and than it itself is no big deal. But the program needs to be able to sort the data in these four arrays by four different criteria, as specified by the user. I've gotten everything down, except for the actual sorting, namely, the sorting of one of the arrays alphabetically.
Any advice on how to sort an array in such a way would be greatly appreciated.
I'm currently working on a program that deals with parallel arrays, and than it itself is no big deal. But the program needs to be able to sort the data in these four arrays by four different criteria, as specified by the user. I've gotten everything down, except for the actual sorting, namely, the sorting of one of the arrays alphabetically.
Any advice on how to sort an array in such a way would be greatly appreciated.
Hum, I think you might be able to just compare the strings in a bubble sort. I remember doing this, but we might have just done it off the first character
Drathys2007-08-28 04:12:29
If efficiency is not a prime concern, then C includes a qsort() function that works on arrays.
buf = array to be sorted
num = number of elements in buf
size = sizeof each element
compare = pointer to a function that takes pointers to two elements, compares them, and returns:
negative if(param1 < param2), zero if(param1 == param2), and positive if(param1 > param2)
C++ also includes a std::sort that works with STL iterators
The first one takes the first and last elements to compare, and compares them using "<". ( (if param1 < param2) return true)
The second one takes a pointer to a function that returns true if the compare condition is met.
For more sorting algorithm info if efficiency is a real issue: http://en.wikipedia.org/wiki/Sorting_algorithm
CODE
void qsort( void *buf, size_t num, size_t size, int (*compare)(const void *, const void *) );
buf = array to be sorted
num = number of elements in buf
size = sizeof each element
compare = pointer to a function that takes pointers to two elements, compares them, and returns:
negative if(param1 < param2), zero if(param1 == param2), and positive if(param1 > param2)
CODE
#include
int compare(const void* param1, const void* param2)
{
    return (param1 - param2);
}
int main()
{
    int sortme = {10, -5, 8, 39, 11, 0};
    qsort(sortme, 6, sizeof(int), compare);
    return 0;
}
int compare(const void* param1, const void* param2)
{
    return (param1 - param2);
}
int main()
{
    int sortme = {10, -5, 8, 39, 11, 0};
    qsort(sortme, 6, sizeof(int), compare);
    return 0;
}
C++ also includes a std::sort that works with STL iterators
CODE
std::sort(iterator start, iterator end);
std::sort(iterator start, iterator end, bool (*compare) (const T*, const T*) );
std::sort(iterator start, iterator end, bool (*compare) (const T*, const T*) );
The first one takes the first and last elements to compare, and compares them using "<". ( (if param1 < param2) return true)
The second one takes a pointer to a function that returns true if the compare condition is met.
CODE
#include
int main()
{
    int sortme = {10, -5, 8, 39, 11, 0};
    std::sort(sortme, sortme + 6);
    return 0;
}
int main()
{
    int sortme = {10, -5, 8, 39, 11, 0};
    std::sort(sortme, sortme + 6);
    return 0;
}
CODE
#include
bool compare(int* param1, int* param2)
{
    return (param2 < param1);
}
int main()
{
    int sortme = {10, -5, 8, 39, 11, 0};
    std::sort(sortme, sortme + 6, compare);
    return 0;
}
bool compare(int* param1, int* param2)
{
    return (param2 < param1);
}
int main()
{
    int sortme = {10, -5, 8, 39, 11, 0};
    std::sort(sortme, sortme + 6, compare);
    return 0;
}
For more sorting algorithm info if efficiency is a real issue: http://en.wikipedia.org/wiki/Sorting_algorithm
Unknown2007-08-28 11:21:16
Unknown2007-08-29 02:03:43
Thanks for all the help guys, but I ended up getting it with a couple for loops and an if statement.
Shout out to Acrune.
Shout out to Acrune.