How to compare two string values from an ArrayList? - arrays

I have an ArrayList that is populated with strings from a text file, and now I want to sort it alphabetically (I can't use collections.sort or anything like that, it has to be manual).
What is the correct way to compare two elements of an ArrayList (if there is one at all)?

Related

Checking an array of PFUsers against another array of PFUsers SWIFT

I have two arrays, arrayA and arrayB which both have the same elements in which are PFUsers. Although arrayB is in the wrong order and needs to be in the order of arrayA. Can someone show me some code which could be able to do this?
You should be able to convert your arrays to sets and use the == operator to compare them, although sets don't allow duplicate objects. If your arrays contain duplicate objects and you want the count of different objects to effect array equality that won't work
You should be able to convert your arrays to NSCountedSet objects, which DO keep track of how many times an object is duplicated.
To sort array B into the order of array A (not particularly dealing with any duplicate objects in either array) you can sort B with a block that finds the indexes of those objects (or objects with the same objectId) and then returns the comparison of the indexes.

How to work with C strings within an Array?

So let's say I have an array of C strings. I'm trying to create a function that I will tell which two strings (probably by index path) to remove and then add a new string (merge of the other two) to the array.
I don't need the actual merging code, just the code that removes the 2 strings by index path, then adds an additional string to the array.
(I will probably need another function to do the actual merge and that's out of the scope of this question.)
You cannot remove a string from an array. You would need to mark the index as being deleted and then copy the entire array to another array omitting the marked elements.

sorting array of string using qsort

So have an assignment that require me to read an input from a file and sort the input. So if I have a file containing:
banana
apple
orange
I have sucessfully insert those string to array. the array contain like this:
{'b','a','n','a','n','a','\n','a','p','p','l','e','\n','o','r','a','n','g','e'}
Now the part that confuse me, is that, I have to use qsort to sort those string. I have create a method that compare two array of char and it works. However, it successfully compare two different string. However, how can I use qsort to sort my array?

compare string to string list in c

in my C application, I have a string which I want to compare to a list of strings in my application. the list of strings can be hardcoded in my C application.
what is the best way to define the list of strings in my application?
what is the best way to compare the string in my application to the list of strings?
I you are after efficiency, you should use a trie, it will give you O(|S|) search time to match your input string to the given set of strings. [where |S| is the length of the input string]
If you are after quick coding, just store the strings in a predefined char*[], and iterate over it with strcmp()
What do you mean by "compare a string to a list of strings"? The two are different kinds of "objects", they are not obviously comparable.
How does the string "372" compare to the list of strings { "12, "42", "-11", "372" }?
If you mean "look for a string in a list of strings", then the best bet would be to sort the list, then use binary-search to quickly check if the candidate is present.
A list of strings is easily represented in C as an array of character pointers, char *strings[]. You can use qsort() to sort, and bsearch() to search.
If you want excessive speed, and you're willing to use extra tools, you could consider gperf. Given a list of strings, it works out a very fast inclusion test based on a hash function, and spits out the C code for it.

C - Arrays/Struct discussion

I have to organize a struct or an array of some people's first name, last name, and age then organize them in alphabetical order writing them from an input file to an output file using the string library.
This is partically for a Lab that I have tomorrow afternoon in which my TA said we might be asked to complete the task I stated above. I'm trying to get opinions or suggestions from individuals whom are much more experienced in C than myself so I'll be more prepared than I was last lab where I didn't so hot.
I'm stuck, any suggestions on where I should start?
Well, reading and writing the input and output should be a no brainer (you open the file, read/write and then close it when you're done with it).
The trick is to get those pesky strings sorted.
The way I look at strnig sorting is an array of arrays sorting. The first array-level is an iteration over all the structs while the second is an iteration over all the letters of a name (note that some names are longer then others)
1. First you sort the first column - all the first letters
2. Then all the sub-columns - within each first letter group you sort the second letter
3. Repeat 1&2 until you run out of letters to sort.
The sorting of an array of letters is the same as sorting an array of byte (AKA chars), and if you're working with an algorithm like bubble sort, working on a sub array is seamless.
Hope this idea helps
You can use strcmp from string.h library, which compares two strings and returns the result.
You should read every string from input file and keep in memory. You will probably use a list for that, for example linked list. While adding you can organize them in alphabetical order. Here is an example:
Take the an element in list.
Compare it with new string.
Where should it be placed? if it should be before the current element, place it before the current element. If it should be after current element, do the same for next element.
This is a simple algorithm for your problem.
The tricky part is to implement the comparation of two strings. the strcmp function only tells you whether the strings are identical. You can loop through the char array to do it.
Once this is done, the remaining sorting is simple.

Resources