Reorganize model array to reflect table row drag and drops - cocoa

I've been racking my brain trying to figure out a way to reorganize my model array when the user commits a drag and drop on an NSTableView. They can currently select one or more row, and drop them before or after any row in the table.
I'm given an IndexSet containing the rows of the table that were dragged. I'm also given a single destinationRow representing the destination of where those rows will be placed.
So for example, the original model looks like this: [0, 1, 2, 3, 4, 5]
And the user drags rows 1 and 2 to after 3...
Then I need to update my array to look like this: [0, 3, 1, 2, 4, 5]
Any ideas on how to efficiently achieve this? Thank you.

I realized I'd been approaching the problem the wrong way.
I was trying to think of how to swap elements with their indexes, when all I really needed to do was use the built in functions .remove(at:) and .insert(obj, at:)
That allowed me to reorganize the array quite simply.

Related

How to show row positional change without a macro

I’d like to know how to show the positional change of data in a table based on the row without using a macro. This is a theoretical question so I don’t have a demo file to share.
Imagine a 3 column (A:C) table sorted by C:C which automatically updates when new data is added via a Google Form. Is it possible in a new column (D:D) to demonstrate the positional change of each row entry based on its previous position?
E.g. if data from row 2 moves to row 4, D4 will = “-2” or if data from row 20 moves to row 10, D10 will = “+10”.
In case it makes a difference, the values in A:A will be unique text. B:C will be calculated numbers.

Google Sheets sum mapped text values by group

I'm looking to count the groups that have at least one occurrence of specific text values in a column. E.g., if any of term_01, term_02, or term_03 are in column B, for 1 or more records associated with a specific value in column A, count 1.
This is not too difficult with a helper column, but I'm trying to do it in one go, with a single formula.
Another way to think about this is that unless every record for a given column A value (e.g. group_01) has a value of term_04 in column B, add 1 to the value displayed in cell I1.
Solution with a helper column:
The helper can be created with an array formula:
Unfortunately, replacing the [sum_range] in SUMIF() with an array fails with "Error: Argument must be a range"
Is there a way to pass an array of values to SUMIF() instead of a range? Am I going about solving this problem the wrong way?
I would still be interested to know if there's a general solution to the question asked in the title, but I solved this specific problem with a slightly different approach.
If the number of records with a term_04 value equals the total number of records for that group, assign 0, otherwise assign 1, and sum the result.
=ArrayFormula(SUM(IF(COUNTIFS($D$3:$D$10, UNIQUE(D3:D10), $E$3:$E$10, $A$6)=(COUNTIF($D$3:$D$10, UNIQUE(D3:D10))), 0, 1)))

Google script - Sheets - sort - how to avoid sorting the first row?

A stupid thing that got me cracking my head. I'm sure you'll laugh, but how do I
sort a sheet without including the first row in the sort?
Here's my code:
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("test1");
spreadsheet.sort(4); // or spreadsheet.sort(4,false); or spreadsheet.sort(4,true);
This sorts by column D, but it also sorts the first row. Column D contains only text.
Funny thing is that if you sort a column which has just numbers/dates, it works and it indeed avoids sorting the first row.
So, how can I sort a column with text and avoid sorting the first row?
I know that I can set a range start at A2 until the last column but this seems "messy". Something like this
spreadsheet.getRange('A2:AI').sort({column: 4, ascending: true});
Thanks
What about this solution:
Freeze the header row and then perform the sorting operation.
In this way it's easy and readable to select the row that will contain the headers of your spreadsheet and you can apply all the sorting operations with a cleaner syntax:
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("test1");
spreadsheet.setFrozenRows(1);
spreadsheet.sort(4);
Reference
.setFrozenRows()
You'll probably find this "messy" too but maybe you could save the first item, sort your column and insert back the first item ?
About the working sort of dates/numbers, maybe the sheet detects that the first cell is the only different one (not a number or a date) and then don't sort it ?

SSRS Sorting Matrix with manualy added column

I have preview of matrix like this:
Columns "Report#" and "Headline Indicator" are added manually, "Report Month" is number of month that is used for row grouping, "1" and "3" are column group fields (department_key_id). Design on the picture below:
I want to sort data by first column "Report#".
I tried to configure some interactive for that column but it was not worked for me(
How can I sort rows with data in the table in this way - 2, 2, 3, 3, 4, 4, 5, 5?
thanks!
So it looks to me like you might need a couple of changes to get the set-up you want here. First, I'm going to recommend adding an ORDER BY [report_month_num] to your query and you can do away with the current row grouping on report_month_num.
You'll need to add a Detail grouping row for each of your four rows. So right click in any textbox, navigate to Add Group > Row Group > Adjacent below. Make sure to select Show detail data for each of the four groups you make. You'll need to copy the data into the new rows and you should be all set.
This should ensure the ordering you are expecting.

Pig - Order by - Different reducers?

I'm new to pig. I'm trying to do a merge join. To meet the following requirement:
Data must be sorted on join keys in ascending (ASC) order on both
sides.
Sample File:
4, The Object of Beauty, 1991,2.8,6150
1, The Nightmare Before Christmas, 1993,3.9,4568
2, The Mummy, 1932,3.5,4388
3, Orphans of the Storm, 1921,3.2,9062
3, Orphans of the Storm, 1921,3.2,9062
4, The Object of Beauty, 1991,2.8,6150
5, Night Tide, 1963,2.8,5126
6, One Magic Christmas, 1985,3.8,5333
7, Muriel's Wedding, 1994,3.5,6323
8, Mother's Boys, 1994,3.4,5733
9, Nosferatu: Original Version, 1929,3.5,5651
10, Nick of Time, 1995,3.4,5333
I executed the following commands, inside PIG:
movies = LOAD 'Sample.csv' using PigStorage (',') as (id: int, name, year, rating, duration);
movies_sorted movies = order by id ASC PARALLEL 3;
movies_sorted store into 'output_movies';
When I execute:
hadoop fs-cat ./output2/part-r-00000
I see that, there are records with equal keys in different partitions. For example, i have the record with id 3, in two different partitions. To my knowledge, records with the same key should always be in the same partition. F
What could be wrong?
In a few cases, including ORDER BY and skewed JOIN, Pig will break the map-reduce convention of sending all records for a given key to just one reducer. (Note that the notion of ordering is already outside the map-reduce paradigm.) You are still guaranteed, however, that if you traverse the output of the reducers in order (as indicated by the number in part-r-NNNNN), the records will be ordered as specified.
You can read more in this thread.

Resources