I’m struggling with an array problem that I’m sure has been solved before. The best analogy I can think of is a product comparison feature where 2-3 products are selected and their characteristics are listed for the user to compare.
I have 2-3 arrays of characteristics. The arrays can have the same characteristics. I need to display a table of the arrays side-by-side where the elements are sorted by “most common.” So, if all three arrays have the same characteristic it would be at the top. Then two similar characteristics would be listed, etc. If two arrays shared a characteristic, the third array would have a blank cell.
The backend is written in PHP. The frontend is Javascript. I am using Underscore for some other functionality, so it’s array functions are available.
I just can’t get my head around the combinations of things I would have to do to make this work! Any help is greatly appreciated!
I managed to find a solution which I think covers this problem, working with the advice from mainegreen in using a secondary structure to record the frequency of each category, and a custom sort function which compares using the values of this frequency array. Example implementation below;
This would work with an array of products as below;