Hey everybody,
I tried to sort a char array according to an other array.
Goal Function (Pseudo):
Correlated (mixed) List:
Array A: {"test3", "", "test2", ""} //nothing in Array ("") equals -1
Array B: {3, -1, 2, -1}// ex. "test3" equals 3, etc.
Finished sorting:
Array A: {"test2", "test3", "", ""} //sort Array A according to values of Array B
Array B: {2, 3, -1, -1}
Unfortunately I was just able to do it with the data-type "String". I need the exact following operation, but with char arrays. When using (large) Strings in my whole program, memory allocations, as well as performance problems appear.
Working Code just for Strings:
#define len(arg) ((unsigned int) (sizeof (arg) / sizeof (arg [0])))// number of items in an array
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
delay(1000);
Serial.println("startup");
//init
int intTest[] = {3, -1, -1, 2,};
String stringTEst[] = {"test3","" , "", "test2" };
//sorting
bubbleAddRef(intTest, stringTEst, len(intTest));
//print-out
for (int i = 0; i < len(intTest); i++) {
Serial.print("intTest: ");
Serial.print(intTest[i]);
Serial.print("- stringTest: ");
Serial.println(stringTEst[i]);
}
}
void loop() {
// put your main code here, to run repeatedly:
}
void bubbleAddRef(int a[], String b[], int size) {//highest goes to the last
//bubble sorting method
//http://hwhacks.com/2016/05/03/bubble-sorting-with-an-arduinoc-application/
for (int i = 0; i < (size - 1); i++) {
for (int o = 0; o < (size - (i + 1)); o++) {
if (((a[o] < a[o + 1]) && (a[o] >= 0)) || (a[o + 1] < 0)) {//values under 0 are seen as the highest
// array a
int t = a[o];
a[o] = a[o + 1];
a[o + 1] = t;
// array b
String st = b[o];
b[o] = b[o + 1];
b[o + 1] = st;
}
}
}
}
I would be really happy if some of you, could help me with this.
Oh yeah, and just doing the same operations with strcpy() for example, is also a performance problem.
I thought about to sort the char array after we determined the needed changes...?
EDIT: You can find the summary and the solution (also different data-types) on the 15# Post!