How to sort and group and divide the array alphabetically in angular2+
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I want to display the content of the buttons according to the alphabetical order and divide them in groups like A-H, I-Q and R-Z. My code is written in angular2+.
My array is -
this.dropdownList = [
{ item_text: 'Organisation' },
{ item_text: 'EwayBill' },
{ item_text: 'SAP' },
{ item_text: 'Collection' },
{ item_text: 'Cancel' },
{ item_text: 'Generate' },
{ item_text: 'Payments' },
{ item_text: 'SMS' },
{ item_text: 'Update' }
]
arrays angular sorting
add a comment |
I want to display the content of the buttons according to the alphabetical order and divide them in groups like A-H, I-Q and R-Z. My code is written in angular2+.
My array is -
this.dropdownList = [
{ item_text: 'Organisation' },
{ item_text: 'EwayBill' },
{ item_text: 'SAP' },
{ item_text: 'Collection' },
{ item_text: 'Cancel' },
{ item_text: 'Generate' },
{ item_text: 'Payments' },
{ item_text: 'SMS' },
{ item_text: 'Update' }
]
arrays angular sorting
add a comment |
I want to display the content of the buttons according to the alphabetical order and divide them in groups like A-H, I-Q and R-Z. My code is written in angular2+.
My array is -
this.dropdownList = [
{ item_text: 'Organisation' },
{ item_text: 'EwayBill' },
{ item_text: 'SAP' },
{ item_text: 'Collection' },
{ item_text: 'Cancel' },
{ item_text: 'Generate' },
{ item_text: 'Payments' },
{ item_text: 'SMS' },
{ item_text: 'Update' }
]
arrays angular sorting
I want to display the content of the buttons according to the alphabetical order and divide them in groups like A-H, I-Q and R-Z. My code is written in angular2+.
My array is -
this.dropdownList = [
{ item_text: 'Organisation' },
{ item_text: 'EwayBill' },
{ item_text: 'SAP' },
{ item_text: 'Collection' },
{ item_text: 'Cancel' },
{ item_text: 'Generate' },
{ item_text: 'Payments' },
{ item_text: 'SMS' },
{ item_text: 'Update' }
]
arrays angular sorting
arrays angular sorting
edited Nov 16 '18 at 15:45
James Z
11.2k71936
11.2k71936
asked Nov 16 '18 at 13:37
TechdiveTechdive
223117
223117
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
This is the solution I came up with. Can be optimised for the cases when we know that the letter ranges don't intersect.
// some basic interfaces to describe types
interface Item {
item_text: string;
}
interface LetterRange {
first: string;
last: string;
}
// the letter ranges
// this solution allows them to intersect
// can adjust for any letter ranges
const ranges: LetterRange = [
{first: 'a', last: 'h'},
{first: 'i', last: 'q'},
{first: 'r', last: 'z'}
];
const dropdownList: Item = [
{ item_text: 'Organisation' },
{ item_text: 'EwayBill' },
{ item_text: 'SAP' },
{ item_text: 'Collection' },
{ item_text: 'Cancel' },
{ item_text: 'Generate' },
{ item_text: 'Payments' },
{ item_text: 'SMS' },
{ item_text: 'Update' }
];
// sort the words alphabetically so that they will appear in the right order
const sorted: Item = dropdownList.sort((a,b) => a.item_text.localeCompare(b.item_text));
// array of grouped items (each group is array as well)
const grouped: Item = sorted.reduce((groups, item) => {
const firstLetter: string = item.item_text.slice(0,1).toLowerCase();
console.log(firstLetter);
// run through each range and check whether the 'firstLetter' fits there
ranges.forEach((range, index) => {
if (firstLetter >= range.first.toLowerCase()
&& firstLetter <= range.last.toLowerCase()) {
groups[index].push(item);
}
});
return groups;
}, new Array(ranges.length).fill()); // initialise with N empty arrays
console.log(grouped);
/*
[
[ { item_text: 'Cancel' },
{ item_text: 'Collection' },
{ item_text: 'EwayBill' },
{ item_text: 'Generate' }
],
[
{ item_text: 'Organisation' },
{ item_text: 'Payments' }
],
[ { item_text: 'SAP' },
{ item_text: 'SMS' },
{ item_text: 'Update' }
]
]
*/
add a comment |
Sort it with a compare function https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
dropdownList.sort(function (item1, item2) {
if (item1.item_text < item2.item_text)
return -1;
if ( item1.item_text > item2.item_text)
return 1;
return 0;
})
or more simple:
dropdownList.sort(function (item1, item2) {
return ('' + item1.item_text).localeCompare(item2.item_text);
})
This is copy pasted with changes from https://stackoverflow.com/a/51169/7329611 with credits to Shog9
After sorting you can slice the array into groups with the slice function https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice
After sorting you should find the indexes of the first occurance of objects with the next higher alphabetical character and set these indexes as slice(from, to)
So for the first step you have to find the first element with next higher Character wich in your case is 'i' since its follows 'h'
let breakpoint = dropdownList.find(function(element) {
return element.item_text.charAt(0).toLowerCase() === 'i';
})
Next you can slice the original array at
let slicePoint = dropdownList.indexOf(breakpoint)-1
dropdownList.slice(slicePoint);
Ofcourse this requires the array to contain at least one element with 'i' at start.
add a comment |
By manipulating the mentioned codes, i framed a code as such -
Proceed() {
// some basic interfaces to describe types
interface Item {
item_text: string;
}
interface LetterRange {
first: string;
last: string;
}
// the letter ranges
// this solution allows them to intersect
// can adjust for any letter ranges
const rangesAtoH: LetterRange = [
{first: 'a', last: 'h'},
];
const rangesItoQ: LetterRange = [
{first: 'i', last: 'q'},
];
const rangesRtoZ: LetterRange = [
{first: 'r', last: 'z'}
];
const dropdownParameter: Item = [
{ item_text: 'Organisation' },
{ item_text: 'EwayBill' },
{ item_text: 'SAP' },
{ item_text: 'Collection' },
{ item_text: 'Cancel' },
{ item_text: 'Generate' },
{ item_text: 'Payments' },
{ item_text: 'SMS' },
{ item_text: 'Update' }
];
// sort the words alphabetically so that they will appear in the right order
const sorted: Item = this.dropdownParameter.sort((a,b) => a.item_text.localeCompare(b.item_text));
console.log("inside Proceed, Sorted = ", sorted)
// -------------------------- Array 2 - suggestion-----------------------------
// array of grouped items (each group is array as well)
const grouped: Item = sorted.reduce((groups, item) => {
const firstLetter: string = item.item_text.slice(0,1).toLowerCase();
console.log(firstLetter);
// run through each range and check whether the 'firstLetter' fits there
rangesAtoH.forEach((range, index) => {
if (firstLetter >= range.first.toLowerCase()
&& firstLetter <= range.last.toLowerCase()) {
groups[index].push(item);
}
});
return groups;
}, new Array(rangesAtoH.length).fill()); // initialise with N empty arrays
console.log("grouped", grouped);
// ---letter range I to Q ------------------
const groupedItoQ: Item = sorted.reduce((groups, item) => {
const firstLetter: string = item.item_text.slice(0,1).toLowerCase();
console.log(firstLetter);
// run through each range and check whether the 'firstLetter' fits there
rangesItoQ.forEach((range, index) => {
if (firstLetter >= range.first.toLowerCase()
&& firstLetter <= range.last.toLowerCase()) {
groups[index].push(item);
}
});
return groups;
}, new Array(rangesItoQ.length).fill()); // initialise with N empty arrays
console.log("grouped I to Q = ", groupedItoQ);
// ---letter range R to Z ------------------
const groupedRtoZ: Item = sorted.reduce((groups, item) => {
const firstLetter: string = item.item_text.slice(0,1).toLowerCase();
console.log(firstLetter);
// run through each range and check whether the 'firstLetter' fits there
rangesRtoZ.forEach((range, index) => {
if (firstLetter >= range.first.toLowerCase()
&& firstLetter <= range.last.toLowerCase()) {
groups[index].push(item);
}
});
return groups;
}, new Array(rangesRtoZ.length).fill()); // initialise with N empty arrays
console.log("grouped I to Q = ", groupedRtoZ);
}
Hmm.. Why did you decide to triple the size of the code I suggested? The point was that you have an array of ranges: [RangeAtoH, RangeItoQ, RangeRtoZ]. With the code I've shown you get the output of wordGroups: [WordsInRangeAtoH, WordsInRangeItoQ, WordsInRangeRtoZ]. This works out of the box. No need to change anything. You can output each group simply by accessing wordGroups array. console.log("grouped A to H = ", wordGroups[0]); console.log("grouped I to Q = ", wordGroups[1]); console.log("grouped R to Z = ", wordGroups[2]);
– Daniil Andreyevich Baunov
Nov 17 '18 at 9:52
No you don't get . All the three arrays have all the values arranged alphabetically. That is why i segregated. Thanks a lot anyway! You solved it originally.
– Techdive
Nov 17 '18 at 10:54
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53338971%2fhow-to-sort-and-group-and-divide-the-array-alphabetically-in-angular2%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
This is the solution I came up with. Can be optimised for the cases when we know that the letter ranges don't intersect.
// some basic interfaces to describe types
interface Item {
item_text: string;
}
interface LetterRange {
first: string;
last: string;
}
// the letter ranges
// this solution allows them to intersect
// can adjust for any letter ranges
const ranges: LetterRange = [
{first: 'a', last: 'h'},
{first: 'i', last: 'q'},
{first: 'r', last: 'z'}
];
const dropdownList: Item = [
{ item_text: 'Organisation' },
{ item_text: 'EwayBill' },
{ item_text: 'SAP' },
{ item_text: 'Collection' },
{ item_text: 'Cancel' },
{ item_text: 'Generate' },
{ item_text: 'Payments' },
{ item_text: 'SMS' },
{ item_text: 'Update' }
];
// sort the words alphabetically so that they will appear in the right order
const sorted: Item = dropdownList.sort((a,b) => a.item_text.localeCompare(b.item_text));
// array of grouped items (each group is array as well)
const grouped: Item = sorted.reduce((groups, item) => {
const firstLetter: string = item.item_text.slice(0,1).toLowerCase();
console.log(firstLetter);
// run through each range and check whether the 'firstLetter' fits there
ranges.forEach((range, index) => {
if (firstLetter >= range.first.toLowerCase()
&& firstLetter <= range.last.toLowerCase()) {
groups[index].push(item);
}
});
return groups;
}, new Array(ranges.length).fill()); // initialise with N empty arrays
console.log(grouped);
/*
[
[ { item_text: 'Cancel' },
{ item_text: 'Collection' },
{ item_text: 'EwayBill' },
{ item_text: 'Generate' }
],
[
{ item_text: 'Organisation' },
{ item_text: 'Payments' }
],
[ { item_text: 'SAP' },
{ item_text: 'SMS' },
{ item_text: 'Update' }
]
]
*/
add a comment |
This is the solution I came up with. Can be optimised for the cases when we know that the letter ranges don't intersect.
// some basic interfaces to describe types
interface Item {
item_text: string;
}
interface LetterRange {
first: string;
last: string;
}
// the letter ranges
// this solution allows them to intersect
// can adjust for any letter ranges
const ranges: LetterRange = [
{first: 'a', last: 'h'},
{first: 'i', last: 'q'},
{first: 'r', last: 'z'}
];
const dropdownList: Item = [
{ item_text: 'Organisation' },
{ item_text: 'EwayBill' },
{ item_text: 'SAP' },
{ item_text: 'Collection' },
{ item_text: 'Cancel' },
{ item_text: 'Generate' },
{ item_text: 'Payments' },
{ item_text: 'SMS' },
{ item_text: 'Update' }
];
// sort the words alphabetically so that they will appear in the right order
const sorted: Item = dropdownList.sort((a,b) => a.item_text.localeCompare(b.item_text));
// array of grouped items (each group is array as well)
const grouped: Item = sorted.reduce((groups, item) => {
const firstLetter: string = item.item_text.slice(0,1).toLowerCase();
console.log(firstLetter);
// run through each range and check whether the 'firstLetter' fits there
ranges.forEach((range, index) => {
if (firstLetter >= range.first.toLowerCase()
&& firstLetter <= range.last.toLowerCase()) {
groups[index].push(item);
}
});
return groups;
}, new Array(ranges.length).fill()); // initialise with N empty arrays
console.log(grouped);
/*
[
[ { item_text: 'Cancel' },
{ item_text: 'Collection' },
{ item_text: 'EwayBill' },
{ item_text: 'Generate' }
],
[
{ item_text: 'Organisation' },
{ item_text: 'Payments' }
],
[ { item_text: 'SAP' },
{ item_text: 'SMS' },
{ item_text: 'Update' }
]
]
*/
add a comment |
This is the solution I came up with. Can be optimised for the cases when we know that the letter ranges don't intersect.
// some basic interfaces to describe types
interface Item {
item_text: string;
}
interface LetterRange {
first: string;
last: string;
}
// the letter ranges
// this solution allows them to intersect
// can adjust for any letter ranges
const ranges: LetterRange = [
{first: 'a', last: 'h'},
{first: 'i', last: 'q'},
{first: 'r', last: 'z'}
];
const dropdownList: Item = [
{ item_text: 'Organisation' },
{ item_text: 'EwayBill' },
{ item_text: 'SAP' },
{ item_text: 'Collection' },
{ item_text: 'Cancel' },
{ item_text: 'Generate' },
{ item_text: 'Payments' },
{ item_text: 'SMS' },
{ item_text: 'Update' }
];
// sort the words alphabetically so that they will appear in the right order
const sorted: Item = dropdownList.sort((a,b) => a.item_text.localeCompare(b.item_text));
// array of grouped items (each group is array as well)
const grouped: Item = sorted.reduce((groups, item) => {
const firstLetter: string = item.item_text.slice(0,1).toLowerCase();
console.log(firstLetter);
// run through each range and check whether the 'firstLetter' fits there
ranges.forEach((range, index) => {
if (firstLetter >= range.first.toLowerCase()
&& firstLetter <= range.last.toLowerCase()) {
groups[index].push(item);
}
});
return groups;
}, new Array(ranges.length).fill()); // initialise with N empty arrays
console.log(grouped);
/*
[
[ { item_text: 'Cancel' },
{ item_text: 'Collection' },
{ item_text: 'EwayBill' },
{ item_text: 'Generate' }
],
[
{ item_text: 'Organisation' },
{ item_text: 'Payments' }
],
[ { item_text: 'SAP' },
{ item_text: 'SMS' },
{ item_text: 'Update' }
]
]
*/
This is the solution I came up with. Can be optimised for the cases when we know that the letter ranges don't intersect.
// some basic interfaces to describe types
interface Item {
item_text: string;
}
interface LetterRange {
first: string;
last: string;
}
// the letter ranges
// this solution allows them to intersect
// can adjust for any letter ranges
const ranges: LetterRange = [
{first: 'a', last: 'h'},
{first: 'i', last: 'q'},
{first: 'r', last: 'z'}
];
const dropdownList: Item = [
{ item_text: 'Organisation' },
{ item_text: 'EwayBill' },
{ item_text: 'SAP' },
{ item_text: 'Collection' },
{ item_text: 'Cancel' },
{ item_text: 'Generate' },
{ item_text: 'Payments' },
{ item_text: 'SMS' },
{ item_text: 'Update' }
];
// sort the words alphabetically so that they will appear in the right order
const sorted: Item = dropdownList.sort((a,b) => a.item_text.localeCompare(b.item_text));
// array of grouped items (each group is array as well)
const grouped: Item = sorted.reduce((groups, item) => {
const firstLetter: string = item.item_text.slice(0,1).toLowerCase();
console.log(firstLetter);
// run through each range and check whether the 'firstLetter' fits there
ranges.forEach((range, index) => {
if (firstLetter >= range.first.toLowerCase()
&& firstLetter <= range.last.toLowerCase()) {
groups[index].push(item);
}
});
return groups;
}, new Array(ranges.length).fill()); // initialise with N empty arrays
console.log(grouped);
/*
[
[ { item_text: 'Cancel' },
{ item_text: 'Collection' },
{ item_text: 'EwayBill' },
{ item_text: 'Generate' }
],
[
{ item_text: 'Organisation' },
{ item_text: 'Payments' }
],
[ { item_text: 'SAP' },
{ item_text: 'SMS' },
{ item_text: 'Update' }
]
]
*/
edited Nov 16 '18 at 14:50
answered Nov 16 '18 at 14:35
Daniil Andreyevich BaunovDaniil Andreyevich Baunov
1,137528
1,137528
add a comment |
add a comment |
Sort it with a compare function https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
dropdownList.sort(function (item1, item2) {
if (item1.item_text < item2.item_text)
return -1;
if ( item1.item_text > item2.item_text)
return 1;
return 0;
})
or more simple:
dropdownList.sort(function (item1, item2) {
return ('' + item1.item_text).localeCompare(item2.item_text);
})
This is copy pasted with changes from https://stackoverflow.com/a/51169/7329611 with credits to Shog9
After sorting you can slice the array into groups with the slice function https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice
After sorting you should find the indexes of the first occurance of objects with the next higher alphabetical character and set these indexes as slice(from, to)
So for the first step you have to find the first element with next higher Character wich in your case is 'i' since its follows 'h'
let breakpoint = dropdownList.find(function(element) {
return element.item_text.charAt(0).toLowerCase() === 'i';
})
Next you can slice the original array at
let slicePoint = dropdownList.indexOf(breakpoint)-1
dropdownList.slice(slicePoint);
Ofcourse this requires the array to contain at least one element with 'i' at start.
add a comment |
Sort it with a compare function https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
dropdownList.sort(function (item1, item2) {
if (item1.item_text < item2.item_text)
return -1;
if ( item1.item_text > item2.item_text)
return 1;
return 0;
})
or more simple:
dropdownList.sort(function (item1, item2) {
return ('' + item1.item_text).localeCompare(item2.item_text);
})
This is copy pasted with changes from https://stackoverflow.com/a/51169/7329611 with credits to Shog9
After sorting you can slice the array into groups with the slice function https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice
After sorting you should find the indexes of the first occurance of objects with the next higher alphabetical character and set these indexes as slice(from, to)
So for the first step you have to find the first element with next higher Character wich in your case is 'i' since its follows 'h'
let breakpoint = dropdownList.find(function(element) {
return element.item_text.charAt(0).toLowerCase() === 'i';
})
Next you can slice the original array at
let slicePoint = dropdownList.indexOf(breakpoint)-1
dropdownList.slice(slicePoint);
Ofcourse this requires the array to contain at least one element with 'i' at start.
add a comment |
Sort it with a compare function https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
dropdownList.sort(function (item1, item2) {
if (item1.item_text < item2.item_text)
return -1;
if ( item1.item_text > item2.item_text)
return 1;
return 0;
})
or more simple:
dropdownList.sort(function (item1, item2) {
return ('' + item1.item_text).localeCompare(item2.item_text);
})
This is copy pasted with changes from https://stackoverflow.com/a/51169/7329611 with credits to Shog9
After sorting you can slice the array into groups with the slice function https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice
After sorting you should find the indexes of the first occurance of objects with the next higher alphabetical character and set these indexes as slice(from, to)
So for the first step you have to find the first element with next higher Character wich in your case is 'i' since its follows 'h'
let breakpoint = dropdownList.find(function(element) {
return element.item_text.charAt(0).toLowerCase() === 'i';
})
Next you can slice the original array at
let slicePoint = dropdownList.indexOf(breakpoint)-1
dropdownList.slice(slicePoint);
Ofcourse this requires the array to contain at least one element with 'i' at start.
Sort it with a compare function https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
dropdownList.sort(function (item1, item2) {
if (item1.item_text < item2.item_text)
return -1;
if ( item1.item_text > item2.item_text)
return 1;
return 0;
})
or more simple:
dropdownList.sort(function (item1, item2) {
return ('' + item1.item_text).localeCompare(item2.item_text);
})
This is copy pasted with changes from https://stackoverflow.com/a/51169/7329611 with credits to Shog9
After sorting you can slice the array into groups with the slice function https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice
After sorting you should find the indexes of the first occurance of objects with the next higher alphabetical character and set these indexes as slice(from, to)
So for the first step you have to find the first element with next higher Character wich in your case is 'i' since its follows 'h'
let breakpoint = dropdownList.find(function(element) {
return element.item_text.charAt(0).toLowerCase() === 'i';
})
Next you can slice the original array at
let slicePoint = dropdownList.indexOf(breakpoint)-1
dropdownList.slice(slicePoint);
Ofcourse this requires the array to contain at least one element with 'i' at start.
edited Nov 16 '18 at 14:26
answered Nov 16 '18 at 13:56
Severin KlugSeverin Klug
294
294
add a comment |
add a comment |
By manipulating the mentioned codes, i framed a code as such -
Proceed() {
// some basic interfaces to describe types
interface Item {
item_text: string;
}
interface LetterRange {
first: string;
last: string;
}
// the letter ranges
// this solution allows them to intersect
// can adjust for any letter ranges
const rangesAtoH: LetterRange = [
{first: 'a', last: 'h'},
];
const rangesItoQ: LetterRange = [
{first: 'i', last: 'q'},
];
const rangesRtoZ: LetterRange = [
{first: 'r', last: 'z'}
];
const dropdownParameter: Item = [
{ item_text: 'Organisation' },
{ item_text: 'EwayBill' },
{ item_text: 'SAP' },
{ item_text: 'Collection' },
{ item_text: 'Cancel' },
{ item_text: 'Generate' },
{ item_text: 'Payments' },
{ item_text: 'SMS' },
{ item_text: 'Update' }
];
// sort the words alphabetically so that they will appear in the right order
const sorted: Item = this.dropdownParameter.sort((a,b) => a.item_text.localeCompare(b.item_text));
console.log("inside Proceed, Sorted = ", sorted)
// -------------------------- Array 2 - suggestion-----------------------------
// array of grouped items (each group is array as well)
const grouped: Item = sorted.reduce((groups, item) => {
const firstLetter: string = item.item_text.slice(0,1).toLowerCase();
console.log(firstLetter);
// run through each range and check whether the 'firstLetter' fits there
rangesAtoH.forEach((range, index) => {
if (firstLetter >= range.first.toLowerCase()
&& firstLetter <= range.last.toLowerCase()) {
groups[index].push(item);
}
});
return groups;
}, new Array(rangesAtoH.length).fill()); // initialise with N empty arrays
console.log("grouped", grouped);
// ---letter range I to Q ------------------
const groupedItoQ: Item = sorted.reduce((groups, item) => {
const firstLetter: string = item.item_text.slice(0,1).toLowerCase();
console.log(firstLetter);
// run through each range and check whether the 'firstLetter' fits there
rangesItoQ.forEach((range, index) => {
if (firstLetter >= range.first.toLowerCase()
&& firstLetter <= range.last.toLowerCase()) {
groups[index].push(item);
}
});
return groups;
}, new Array(rangesItoQ.length).fill()); // initialise with N empty arrays
console.log("grouped I to Q = ", groupedItoQ);
// ---letter range R to Z ------------------
const groupedRtoZ: Item = sorted.reduce((groups, item) => {
const firstLetter: string = item.item_text.slice(0,1).toLowerCase();
console.log(firstLetter);
// run through each range and check whether the 'firstLetter' fits there
rangesRtoZ.forEach((range, index) => {
if (firstLetter >= range.first.toLowerCase()
&& firstLetter <= range.last.toLowerCase()) {
groups[index].push(item);
}
});
return groups;
}, new Array(rangesRtoZ.length).fill()); // initialise with N empty arrays
console.log("grouped I to Q = ", groupedRtoZ);
}
Hmm.. Why did you decide to triple the size of the code I suggested? The point was that you have an array of ranges: [RangeAtoH, RangeItoQ, RangeRtoZ]. With the code I've shown you get the output of wordGroups: [WordsInRangeAtoH, WordsInRangeItoQ, WordsInRangeRtoZ]. This works out of the box. No need to change anything. You can output each group simply by accessing wordGroups array. console.log("grouped A to H = ", wordGroups[0]); console.log("grouped I to Q = ", wordGroups[1]); console.log("grouped R to Z = ", wordGroups[2]);
– Daniil Andreyevich Baunov
Nov 17 '18 at 9:52
No you don't get . All the three arrays have all the values arranged alphabetically. That is why i segregated. Thanks a lot anyway! You solved it originally.
– Techdive
Nov 17 '18 at 10:54
add a comment |
By manipulating the mentioned codes, i framed a code as such -
Proceed() {
// some basic interfaces to describe types
interface Item {
item_text: string;
}
interface LetterRange {
first: string;
last: string;
}
// the letter ranges
// this solution allows them to intersect
// can adjust for any letter ranges
const rangesAtoH: LetterRange = [
{first: 'a', last: 'h'},
];
const rangesItoQ: LetterRange = [
{first: 'i', last: 'q'},
];
const rangesRtoZ: LetterRange = [
{first: 'r', last: 'z'}
];
const dropdownParameter: Item = [
{ item_text: 'Organisation' },
{ item_text: 'EwayBill' },
{ item_text: 'SAP' },
{ item_text: 'Collection' },
{ item_text: 'Cancel' },
{ item_text: 'Generate' },
{ item_text: 'Payments' },
{ item_text: 'SMS' },
{ item_text: 'Update' }
];
// sort the words alphabetically so that they will appear in the right order
const sorted: Item = this.dropdownParameter.sort((a,b) => a.item_text.localeCompare(b.item_text));
console.log("inside Proceed, Sorted = ", sorted)
// -------------------------- Array 2 - suggestion-----------------------------
// array of grouped items (each group is array as well)
const grouped: Item = sorted.reduce((groups, item) => {
const firstLetter: string = item.item_text.slice(0,1).toLowerCase();
console.log(firstLetter);
// run through each range and check whether the 'firstLetter' fits there
rangesAtoH.forEach((range, index) => {
if (firstLetter >= range.first.toLowerCase()
&& firstLetter <= range.last.toLowerCase()) {
groups[index].push(item);
}
});
return groups;
}, new Array(rangesAtoH.length).fill()); // initialise with N empty arrays
console.log("grouped", grouped);
// ---letter range I to Q ------------------
const groupedItoQ: Item = sorted.reduce((groups, item) => {
const firstLetter: string = item.item_text.slice(0,1).toLowerCase();
console.log(firstLetter);
// run through each range and check whether the 'firstLetter' fits there
rangesItoQ.forEach((range, index) => {
if (firstLetter >= range.first.toLowerCase()
&& firstLetter <= range.last.toLowerCase()) {
groups[index].push(item);
}
});
return groups;
}, new Array(rangesItoQ.length).fill()); // initialise with N empty arrays
console.log("grouped I to Q = ", groupedItoQ);
// ---letter range R to Z ------------------
const groupedRtoZ: Item = sorted.reduce((groups, item) => {
const firstLetter: string = item.item_text.slice(0,1).toLowerCase();
console.log(firstLetter);
// run through each range and check whether the 'firstLetter' fits there
rangesRtoZ.forEach((range, index) => {
if (firstLetter >= range.first.toLowerCase()
&& firstLetter <= range.last.toLowerCase()) {
groups[index].push(item);
}
});
return groups;
}, new Array(rangesRtoZ.length).fill()); // initialise with N empty arrays
console.log("grouped I to Q = ", groupedRtoZ);
}
Hmm.. Why did you decide to triple the size of the code I suggested? The point was that you have an array of ranges: [RangeAtoH, RangeItoQ, RangeRtoZ]. With the code I've shown you get the output of wordGroups: [WordsInRangeAtoH, WordsInRangeItoQ, WordsInRangeRtoZ]. This works out of the box. No need to change anything. You can output each group simply by accessing wordGroups array. console.log("grouped A to H = ", wordGroups[0]); console.log("grouped I to Q = ", wordGroups[1]); console.log("grouped R to Z = ", wordGroups[2]);
– Daniil Andreyevich Baunov
Nov 17 '18 at 9:52
No you don't get . All the three arrays have all the values arranged alphabetically. That is why i segregated. Thanks a lot anyway! You solved it originally.
– Techdive
Nov 17 '18 at 10:54
add a comment |
By manipulating the mentioned codes, i framed a code as such -
Proceed() {
// some basic interfaces to describe types
interface Item {
item_text: string;
}
interface LetterRange {
first: string;
last: string;
}
// the letter ranges
// this solution allows them to intersect
// can adjust for any letter ranges
const rangesAtoH: LetterRange = [
{first: 'a', last: 'h'},
];
const rangesItoQ: LetterRange = [
{first: 'i', last: 'q'},
];
const rangesRtoZ: LetterRange = [
{first: 'r', last: 'z'}
];
const dropdownParameter: Item = [
{ item_text: 'Organisation' },
{ item_text: 'EwayBill' },
{ item_text: 'SAP' },
{ item_text: 'Collection' },
{ item_text: 'Cancel' },
{ item_text: 'Generate' },
{ item_text: 'Payments' },
{ item_text: 'SMS' },
{ item_text: 'Update' }
];
// sort the words alphabetically so that they will appear in the right order
const sorted: Item = this.dropdownParameter.sort((a,b) => a.item_text.localeCompare(b.item_text));
console.log("inside Proceed, Sorted = ", sorted)
// -------------------------- Array 2 - suggestion-----------------------------
// array of grouped items (each group is array as well)
const grouped: Item = sorted.reduce((groups, item) => {
const firstLetter: string = item.item_text.slice(0,1).toLowerCase();
console.log(firstLetter);
// run through each range and check whether the 'firstLetter' fits there
rangesAtoH.forEach((range, index) => {
if (firstLetter >= range.first.toLowerCase()
&& firstLetter <= range.last.toLowerCase()) {
groups[index].push(item);
}
});
return groups;
}, new Array(rangesAtoH.length).fill()); // initialise with N empty arrays
console.log("grouped", grouped);
// ---letter range I to Q ------------------
const groupedItoQ: Item = sorted.reduce((groups, item) => {
const firstLetter: string = item.item_text.slice(0,1).toLowerCase();
console.log(firstLetter);
// run through each range and check whether the 'firstLetter' fits there
rangesItoQ.forEach((range, index) => {
if (firstLetter >= range.first.toLowerCase()
&& firstLetter <= range.last.toLowerCase()) {
groups[index].push(item);
}
});
return groups;
}, new Array(rangesItoQ.length).fill()); // initialise with N empty arrays
console.log("grouped I to Q = ", groupedItoQ);
// ---letter range R to Z ------------------
const groupedRtoZ: Item = sorted.reduce((groups, item) => {
const firstLetter: string = item.item_text.slice(0,1).toLowerCase();
console.log(firstLetter);
// run through each range and check whether the 'firstLetter' fits there
rangesRtoZ.forEach((range, index) => {
if (firstLetter >= range.first.toLowerCase()
&& firstLetter <= range.last.toLowerCase()) {
groups[index].push(item);
}
});
return groups;
}, new Array(rangesRtoZ.length).fill()); // initialise with N empty arrays
console.log("grouped I to Q = ", groupedRtoZ);
}
By manipulating the mentioned codes, i framed a code as such -
Proceed() {
// some basic interfaces to describe types
interface Item {
item_text: string;
}
interface LetterRange {
first: string;
last: string;
}
// the letter ranges
// this solution allows them to intersect
// can adjust for any letter ranges
const rangesAtoH: LetterRange = [
{first: 'a', last: 'h'},
];
const rangesItoQ: LetterRange = [
{first: 'i', last: 'q'},
];
const rangesRtoZ: LetterRange = [
{first: 'r', last: 'z'}
];
const dropdownParameter: Item = [
{ item_text: 'Organisation' },
{ item_text: 'EwayBill' },
{ item_text: 'SAP' },
{ item_text: 'Collection' },
{ item_text: 'Cancel' },
{ item_text: 'Generate' },
{ item_text: 'Payments' },
{ item_text: 'SMS' },
{ item_text: 'Update' }
];
// sort the words alphabetically so that they will appear in the right order
const sorted: Item = this.dropdownParameter.sort((a,b) => a.item_text.localeCompare(b.item_text));
console.log("inside Proceed, Sorted = ", sorted)
// -------------------------- Array 2 - suggestion-----------------------------
// array of grouped items (each group is array as well)
const grouped: Item = sorted.reduce((groups, item) => {
const firstLetter: string = item.item_text.slice(0,1).toLowerCase();
console.log(firstLetter);
// run through each range and check whether the 'firstLetter' fits there
rangesAtoH.forEach((range, index) => {
if (firstLetter >= range.first.toLowerCase()
&& firstLetter <= range.last.toLowerCase()) {
groups[index].push(item);
}
});
return groups;
}, new Array(rangesAtoH.length).fill()); // initialise with N empty arrays
console.log("grouped", grouped);
// ---letter range I to Q ------------------
const groupedItoQ: Item = sorted.reduce((groups, item) => {
const firstLetter: string = item.item_text.slice(0,1).toLowerCase();
console.log(firstLetter);
// run through each range and check whether the 'firstLetter' fits there
rangesItoQ.forEach((range, index) => {
if (firstLetter >= range.first.toLowerCase()
&& firstLetter <= range.last.toLowerCase()) {
groups[index].push(item);
}
});
return groups;
}, new Array(rangesItoQ.length).fill()); // initialise with N empty arrays
console.log("grouped I to Q = ", groupedItoQ);
// ---letter range R to Z ------------------
const groupedRtoZ: Item = sorted.reduce((groups, item) => {
const firstLetter: string = item.item_text.slice(0,1).toLowerCase();
console.log(firstLetter);
// run through each range and check whether the 'firstLetter' fits there
rangesRtoZ.forEach((range, index) => {
if (firstLetter >= range.first.toLowerCase()
&& firstLetter <= range.last.toLowerCase()) {
groups[index].push(item);
}
});
return groups;
}, new Array(rangesRtoZ.length).fill()); // initialise with N empty arrays
console.log("grouped I to Q = ", groupedRtoZ);
}
answered Nov 17 '18 at 9:20
TechdiveTechdive
223117
223117
Hmm.. Why did you decide to triple the size of the code I suggested? The point was that you have an array of ranges: [RangeAtoH, RangeItoQ, RangeRtoZ]. With the code I've shown you get the output of wordGroups: [WordsInRangeAtoH, WordsInRangeItoQ, WordsInRangeRtoZ]. This works out of the box. No need to change anything. You can output each group simply by accessing wordGroups array. console.log("grouped A to H = ", wordGroups[0]); console.log("grouped I to Q = ", wordGroups[1]); console.log("grouped R to Z = ", wordGroups[2]);
– Daniil Andreyevich Baunov
Nov 17 '18 at 9:52
No you don't get . All the three arrays have all the values arranged alphabetically. That is why i segregated. Thanks a lot anyway! You solved it originally.
– Techdive
Nov 17 '18 at 10:54
add a comment |
Hmm.. Why did you decide to triple the size of the code I suggested? The point was that you have an array of ranges: [RangeAtoH, RangeItoQ, RangeRtoZ]. With the code I've shown you get the output of wordGroups: [WordsInRangeAtoH, WordsInRangeItoQ, WordsInRangeRtoZ]. This works out of the box. No need to change anything. You can output each group simply by accessing wordGroups array. console.log("grouped A to H = ", wordGroups[0]); console.log("grouped I to Q = ", wordGroups[1]); console.log("grouped R to Z = ", wordGroups[2]);
– Daniil Andreyevich Baunov
Nov 17 '18 at 9:52
No you don't get . All the three arrays have all the values arranged alphabetically. That is why i segregated. Thanks a lot anyway! You solved it originally.
– Techdive
Nov 17 '18 at 10:54
Hmm.. Why did you decide to triple the size of the code I suggested? The point was that you have an array of ranges: [RangeAtoH, RangeItoQ, RangeRtoZ]. With the code I've shown you get the output of wordGroups: [WordsInRangeAtoH, WordsInRangeItoQ, WordsInRangeRtoZ]. This works out of the box. No need to change anything. You can output each group simply by accessing wordGroups array. console.log("grouped A to H = ", wordGroups[0]); console.log("grouped I to Q = ", wordGroups[1]); console.log("grouped R to Z = ", wordGroups[2]);
– Daniil Andreyevich Baunov
Nov 17 '18 at 9:52
Hmm.. Why did you decide to triple the size of the code I suggested? The point was that you have an array of ranges: [RangeAtoH, RangeItoQ, RangeRtoZ]. With the code I've shown you get the output of wordGroups: [WordsInRangeAtoH, WordsInRangeItoQ, WordsInRangeRtoZ]. This works out of the box. No need to change anything. You can output each group simply by accessing wordGroups array. console.log("grouped A to H = ", wordGroups[0]); console.log("grouped I to Q = ", wordGroups[1]); console.log("grouped R to Z = ", wordGroups[2]);
– Daniil Andreyevich Baunov
Nov 17 '18 at 9:52
No you don't get . All the three arrays have all the values arranged alphabetically. That is why i segregated. Thanks a lot anyway! You solved it originally.
– Techdive
Nov 17 '18 at 10:54
No you don't get . All the three arrays have all the values arranged alphabetically. That is why i segregated. Thanks a lot anyway! You solved it originally.
– Techdive
Nov 17 '18 at 10:54
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53338971%2fhow-to-sort-and-group-and-divide-the-array-alphabetically-in-angular2%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown