0

I have a little problem to solve. I want to compare two texts if they are the same.

let currentValue = "24960307.W 25880305.W 24880208.W 25650156.W"

let newValue = "24880208.W 24960307.W 25650156.W 25880305.W"

// is the same text just diferent order

// when i did includes 
let x = currentValue.includes(value);
console.log(x);


//response in console
false

I tried with includes and localeCompare but still show that text is different.

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Sejooo
  • 11
  • 1
  • 3
    They _are_ different. How exactly are they supposed to be compared? What’s the rule? – Sebastian Simon Nov 14 '22 at 14:56
  • What's `value` in that code? I mean... you have to check each **part** of the two strings if order is not important and you're stuck with string representation. This isn't the same text, but may be the same **value** for your purposes. – Dave Newton Nov 14 '22 at 15:00
  • With "is the same text just diferent [sic] order" are you referring to the different parts of the strings? I think you want to `split` the strings on, say, a space, and compare the arrays. – Heretic Monkey Nov 14 '22 at 15:01
  • @SebastianSimon rule is just i want to compare this 2 tests value must be the same doesnt mether if "word" come first second or last". – Sejooo Nov 14 '22 at 15:23
  • @HereticMonkey sorry i just start to learn JS i dont know to specific(professional) to explain. i just can say what i wan to do. I got anwser form #Tim Biegeleisen and work just i want. – Sejooo Nov 14 '22 at 15:26

1 Answers1

0

A quick solution to normalize order is to split, sort, rejoin, then compare. This avoids any fancy parsing.

let currentValue = "24960307.W 25880305.W 24880208.W 25650156.W"
let newValue = "25880305.W 24880208.W 25650156.W 24960307.W"


const a = currentValue.split(' ').sort().join(' ')
const b = newValue.split(' ').sort().join(' ')

console.log(a === b)

let x = a.localeCompare(b) === 0
console.log(x);
Steven Spungin
  • 27,002
  • 5
  • 88
  • 78