Add lists_sort block.

This commit is contained in:
Dan LaLiberte
2016-05-04 17:03:24 -04:00
parent 7faafe0b6d
commit c1cfec8dbd
83 changed files with 3039 additions and 2161 deletions

View File

@@ -356,6 +356,34 @@ Blockly.PHP['lists_getSublist'] = function(block) {
return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
};
Blockly.PHP['lists_sort'] = function(block) {
// Block for sorting a list.
var listCode = Blockly.PHP.valueToCode(block, 'LIST',
Blockly.PHP.ORDER_FUNCTION_CALL) || 'array()';
var direction = block.getFieldValue('DIRECTION') === '1' ? 1 : -1;
var type = block.getFieldValue('TYPE');
var functionName = Blockly.PHP.provideFunction_(
'lists_sort', [
'function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ +
'($list, $type, $direction) {',
' $sortFuncs = array(',
' "NUMERIC" => "sort",',
' "TEXT" => "natsort",',
' "IGNORE_CASE" => "natcasesort"',
' );',
' $sort = $sortFuncs[$type];',
' $list2 = $list;', // Clone list.
' $sort($list2);',
' if ($direction == -1) {',
' $list2 = array_reverse($list2);',
' }',
' return $list2;',
'}']);
var sortCode = functionName +
'(' + listCode + ', "' + type + '", ' + direction + ')';
return [sortCode, Blockly.PHP.ORDER_FUNCTION_CALL];
};
Blockly.PHP['lists_split'] = function(block) {
// Block for splitting text into a list, or joining a list into text.
var value_input = Blockly.PHP.valueToCode(block, 'INPUT',