A pike or long spear beats a sword, but sword plus shield was a very viable alternative if you could force your way past the range advantage (usually with some losses). The spear benefits from order and organization, the sword works best in a state of chaos. Rome started out with spears and hastas (a knife bladed spear-like weapon), but after several painful battles against Gauls with sword and shield, decided to go that route instead. Problem was, it took a LOT of discipline and high morale to get a line of men to gather the courage to push past the spears into effective sword range. When it worked, the sword proved decisive; where it failed to push past, or the troops wavered, the spears won.
Rome still used the spear as a defensive weapon in many engagements, and its fabled (and very misunderstood) Triarii were armed with spears, not swords: they were generally the "specialists" in the army, such as smiths, paymasters, clerks, engineers, head cooks, or others with a lifetime of service and skills that were hard to replace, NOT the elite fighting force that many game players use them as. If they had to get involved, holding the enemy back with spears while the youngsters tried to reorganize the defense, it was already a bad day. With large pensions at stake (a defeated Legion might be disbanded, forfeiting all of its accumulated pensions and loot), the Triarii were certainly high-morale troops (it's amazing how much a risk to 20-30 years of accumulated pension money can motivate someone), but many were past their prime or were never that good at combat (picture fighting against a professional accountant).
The spear in formation depends on having enough points focused on the enemy to stop him from blocking all of them, or the intimidation factor preventing him from getting past the business end of the spears. That requires multiple lines. The sword doesn't need multiple lines to be effective, since it's faster for striking at different locations to negate a shield's coverage. You can stab several times in succession with a spear, but it takes more time to strike a different area, so it requires more than one spear targeting different locations to effectively negate the safety of a shield. Since the spear has greater range, that's possible, given more ranks in formation.
One on one, spear versus sword is probably marginally better with the spear, almost entirely on account of reach. Sword and shield versus spear and shield takes away the spear's strong points and hands the advantage to the sword. Fighting in formation with a larger group, the spear again takes the top role, with swords often relegated to exploiting breaks in the opposing line or fighting on the flanks.
I guess the effectiveness of spears may depend on formation AI, and whether the troops can form ranks instead of a skirmish line.