The visible outcome of the two commands ls | sort file.txt and sort file.txt would be the same:
sort file.txt sorts the lines of file.txt lexicographically and outputs the result.
ls | sort file.txt calls ls to generate a list of the names in the current directory. That list is sent to sort file.txt. Since sort is reading from file.txt, it will ignore the list of names coming from ls and instead produce the sorted contents of file.txt as output. The output from ls is discarded since sort is not reading from its standard input in this instance.
You may have wanted to use ls | sort which would have sorted the lines of output from ls (which would have been sorted already). sort would have read the output of ls since it was not given any specific filename to read from and is therefore reading from its standard input stream (which is connected to the output stream of ls via the pipe). Note though that using a tool that expects lines of text may fail to handle Unix filenames as these may contain newlines.
ls. See Why not parsels(and what to do instead)? – phuclv Aug 14 '20 at 14:18lsis being discarded. – jesse_b Aug 14 '20 at 14:35ls | sort file.txtwill spawn an extra process – jesse_b Aug 14 '20 at 14:58