AngularJS sorting a list with nulls - angularjs

I have a scope object that has a list containing null values, I would like to sort this list in both directions but the null values must be always at the end. I can probably do this easily by splitting up the object into 2, one with nulls and one without then stick on the null list at the end. But is there a more efficient way to do this?
example:
[1,null,5,7,2,null]
sorted:
ASD: 1,2,5,7,null,null
DESC: 7,5,2,1,null,null

If you just want to sort the array, JavaScript has method array.sort. This takes a comparer function.
You can implement two of such functions one for ascending sorting and one for descending. Within these two functions you can treat nulls as you wish. See documentation here
If you are using angularjs filter orderby it too takes a comparer function, so a similar approach may work.

Related

Merge two Eloquent Collections and remove all duplicates.

I have two arrays, $user_roles and $global_roles. I want to make a new array, let's call it $available_roles, where it can be equated as the items in $global_roles less the items in the $user_roles
I have the following code to do it to a normal array. $available_roles = array_unique(array_merge($global_roles, $user_roles), SORT_REGULAR);
This is proving to be problematic due to the fact that Laravel does not use traditional arrays when one executes a query, it uses Eloquent Collections.
What other ideas do you guys have?
This is fairly simple. You can use the Collection's merge method:
$available_roles = $global_roles->merge($user_roles);
Because merge internally uses an associative array (dictionary) that uses the id as key, this should automatically remove duplicates.
Anyways though, you can remove duplicates in a collection using unique:
$uniqueCollection = $collection->unique();
Now that was for merging what you're actually are looking for is the difference between two collections. You can do this in two ways:
$available_roles = $user_roles->diff($global_roles);
or
$available_roles = $global_roles->except($user_roles->modelKeys());

Is it more efficient to filter an array or a list in Scala?

Suppose I have a long String str, and I want to split it up, and filter out non-empty elements, and end up with a List[String].
Which is more efficient of these two?
str.split(",").filter(_.trim.nonEmpty).toList
str.split(",").toList.filter(_.trim.nonEmpty)
In the first case, I filter the Array, and then turn it into a List. In the second, I turn it into a List, and then filter it.
There doesn't seem anything to be said for one over the other in terms of readability.

Sort Names Alphabetically in C

I have a C structure that contains contact info for a person, such as name, phone number, etc. The "contact" structures are contained within a linked list. I need to insert nodes in a way such that the linked list is sorted into alphabetical (ascending) order.
Is there a built-in sorting function in C that I can call? Or do I have to write my own sorting function? If there is a built-in function, could I get an example of how I'd call it on a structure within a linked list?
There's no standard sorting method for a "list". The closest is qsort (which can indeed sort user-defined objects) but it only works on continuous ranges (arrays and the like).
You'll probably have to implement your own sorting procedure or use and array instead of a list.
Here is an example of code that performs an insertion sorted linked list. This isn't a cut and paste for you, but will show you what to expect in that type of insertion:
http://www.c.happycodings.com/Sorting_Searching/code8.html
Focus on the "insert()" call. I did not compile this code to test it, but I read it over and it looks correct to me. It shows how to search a list and pointer adjustments. You should be able to solve your problem from this code.

Skip column in an array

I have a VBA function that returns an array to be displayed in Excel. The array's first two columns contain ID's that don't need to be displayed.
Is there any way to modify the Excel formula to skip the first two columns, without going back to create a VBA helper to strip off the columns?
The formula looks like this, where the brackets let the array be displayed across a span of cells:
{=GetCustomers($a$1)}
The closest thing Excel has to built-in array manipulation is the 'INDEX' function. In your case, if the array returned by your 'GetCustomers' routine is a single row, and if you know how long it is (which I guess you do since you're putting it into the sheet), you can get what you want by doing something like this:
=INDEX(GetCustomers($A$1),{3,4,5})
So say GetCustomers() returned the array {1,2,"a","b","c"}, the above would just give back {"a","b","c"}.
There are various ways to save yourself having to type out your array of indices, though. For example,
=COLUMN(C1:E1)
will return {3,4,5}, and you can use that instead:
=INDEX(GetCustomers($A$1),COLUMN(C1:E1))
This trick doesn't work with a true 2-D array, though. 'INDEX' will return a whole row or column if you pass in a zero in the right place, but I don't know how to make it return a 2-D subset. EDIT: You can do it, but it's cumbersome. Say your array is 2x5, and you want the last three columns. You could use:
=INDEX(GetCustomers($A$1), {1,1,1;2,2,2}, {3,4,5;3,4,5})
(FURTHER EDIT: chris neilsen provides a nice way to compute those arrays in his answer.)
Charles Williams has a link on his website that explains more about using arrays like this here:
http://www.decisionmodels.com/optspeedj.htm
He posted that in response to this question I asked:
Is there any documentation of the behavior of built-in Excel functions called with array arguments?
Personally, I use VBA helper functions for things like this. With the right library routines, you can do something like:
=subseq(GetCustomers($A$1),3,3)
in the 1-D case, or:
=hstack(subseq(asCols(GetCustomers($A$1)),3,3))
in the 2-D case, and it's much more clear.
simplest solution is to just hide the first two columns
another may be to use OFFSET to resize the returned array
syntax is
OFFSET(reference,rows,cols,height,width)
I suggest modifying the 'GetCustomers' function to include an optional Boolean variable to tell the function to return all the columns, or just the single column. That would be the cleanest solution, instead of trying to handle it on the formula side.
Public Function GetCustomers(rng as Range, Optional stripColumns as Boolean = False) as Variant()
If stripColumns Then 'Resize array to meet your needs
Else 'return full sized array
End If
End Function
You can use the INDEX function to extract items from the return array
- formula is in an range starting at cell B2
{=INDEX(getcustomers($A$1),ROW()-ROW($B$2)+1,COLUMN()-COLUMN($B$2)+3)}

Sorting an array arbitrarily in cakephp

I have an array which is dynamic, and key and values are displaying in order if they are set. I want to display keys of this array in my own order, What kind of condition must I write in this case?
Take a look at the Core Utility Libraries, namely the sort() method of the Set class. Check for more info the cookbook.

Resources